Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

LINQ datetime comparison in where clause

$
0
0

i have a nullable birthdate column in sql server which i am using to compare age search criteria in linq to sql.Now the problem is, i can't compare the property on the nullable type which is 'year' in my linq-2-sql query's where clause.

i am trying to do something like this....

query.where ( item = > datetime.now.year - item.birthdate.value.year == searchcriteria.age);

the above statement fails because sometimes there are nulls in item.birthdate so calling value on it fails.I have tried testing for item.birthdate!=null before i do the comparison for age like

query.where ( item = > item.birthdate!=null && datetime.now.year - item.birthdate.value.year == searchcriteria.age);

but this doesn't help too as i think i get the error because of the order in which the query expression is evaluated.

thanks,


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>