c# - Find row via As Of date in a DataTable -
i have datatable following structure (showing sample data 1 employee):
employee date payrate -------------------------------- 123 1/1/2015 15.00 123 1/1/2014 14.50 123 1/1/2013 14.00
i need able use "as of" date select pay rate given given date. if pass in emp# 123 , 1/5/2014, should $14.50. if pass in emp# 123 , 7/8/2013, should $14.00 , if use emp# 123 , today's date, should $15.00 , on.
i know how in pure sql, i'm stuck @ approach doing in pure system.data.datatable. have been out of development while, , never did learn use linq (and suspect it's easy in linq).
i thought of approach of looping through table until find first record date less "as-of" date matching employee# seems inelegant way handle this.
i thought of using
datatable.select("date > #" + asofdate.tostring("mm/dd/yyyy") + "#", "asofdate");
to datarow[] array, , selecting first value if returns more 0 rows, seems hack.
what standard/best practice/recommended approach these days?
if matters, i'm using .net 4.5
sort rows date in descending order , find first 1 of data smaller reference date:
datatable.asenumerable() .where(r => r.field<int>("employee") == 123) .orderbydescending (r => r.field<datetime>("date")) .firstordefault (r => r.field<datetime>("date") < new datetime(2013, 7, 8) )
Comments
Post a Comment