ASP.NET - LINQ filter with datetime

Asked By Sagar P on 01-Nov-10 05:59 AM
Hello All,
I am working on MVC application. in that i have one LINQ query where i am filtering data with datevalue..

For that i have written this code;

offers = offers.Where(x => arrStartDt.Contains(x.StartDate.ToString("MM/yyyy")));

In this arrStartDt is a array of date like Month/Year like;
01/2009
02/2010
04/2009

etc.

x.StartDate is a DateTime.....

So i want to filter all the data which has start date in these months with year....

But above statement throws error like this;

Method 'System.String ToString(System.String)' has no supported translation to SQL.

Can you please help me in this filter?????
Nowshad M replied to Sagar P on 01-Nov-10 06:05 AM
Hi,
May i know wats the value you are getting for arrStartDt and x.StartDate?
Sagar P replied to Nowshad M on 01-Nov-10 06:07 AM
Thanks for reply.... i have already mentioned in post...

In this arrStartDt is a array of date like Month/Year like;
01/2009
02/2010
04/2009

And x.StartDate is a datetime column of my sql db table...... it has proper value
Nowshad M replied to Sagar P on 01-Nov-10 06:13 AM
Hi,
Then you cannot format sql date like this
x.StartDate.ToString("MM/yyyy")
Try like this,
string date=x.StartDate.ToString();
 date=date.split('/')[0]+"/"+ date.split('/')[2] //If your date is in mm/dd/yyyy format. If any other format change the splitting values accroding to that.

offers = offers.Where(x => arrStartDt.Contains(date)); 

Hope this helps.