c# - Date field comparison in linq DataService Query -


i have wcf data service fetch customer records table has datetime column well. in ui have grid displays name , dob.i have text box filter. when there text entered in filter, want text compared against columns in table. getting exception when search text compared against datetime column. query using below.

enter code here db.customers.where(customer => customer.lastname.toupper().startswith(text) ||                                customer.firstnme.toupper().startswith(text) ||                                (customer.dob.value.year.tostring()                                                        .startswith(text)                   ); 

this throws exception 'error translating linq expression uri'.

on other hand if compare datetime field direct value works.

enter code here  db.customers.where(customer => customer.lastname.toupper().startswith(text) ||                    customer.firstnme.toupper().startswith(text) ||                    (customer.dob.value.year == 1985)                   ); 

how can compare datetime column entered text??

you convert entered text integer , replace literal in second query it:

int year; if(!int.tryparse(text, out year)) year = -1; var res = db.customers.where(customer =>      customer.lastname.toupper().startswith(text) ||     customer.firstnme.toupper().startswith(text) ||     (customer.dob.value.year == year) ); 

by setting year = -1 in case of data not being numeric, guarantees nothing in particular date field match.

of course, assumes you're interested in comparing against year property. if needed compare full datetimes, parse entered text datetime, use comparisons.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -