c# - Querying from a DataTable to retrieve information based on DateTime -


my question might bit long coz i've tried explain context behind it.

i have datatable (retrieved database) contains following data:

---------------------------------- -  state -         date          - ================================== -   on   -  02/01/2016 15:38:13  - ---------------------------------- -   off  -  22/01/2016 16:45:33  - ---------------------------------- -   on   -  27/01/2016 10:30:16  - ---------------------------------- -   off  -  01/02/2016 10:03:43  - ---------------------------------- 

i need check state depending on date. date in between have previous date's state. example: 29/01/2016 have state 'on'.

i tried query datatable such :

datetime test = datetime.parse("27 / 01 / 2016"); //temporary part var queryresults = table.asenumerable()                            .where(x => datetime.parse(x.field<string>("date")).date == test); 

right query above returns row condition fulfilled. if change statement in query where(x => datetime.parse(x.field<string>("date")).date <= test); returns first 3 rows.

my question .. if change test value 28/01/2016 less or equal query still return first 3 rows. how can last 1 only? last state.

any appreciated :)

use .lastordefault() last item in collection

var queryresults = table.where(x => datetime.parse(x.field<string>("date")).date <= test).lastordefault(); 

sid enote: should check null before accessing state property


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -