B Vidhya B Vidhya - 1 month ago 15
ASP.NET (C#) Question

Retrieve rows between the date range based on total days

I've a IQueryable result queried from the db using LINQ, now I had to filter all the rows based on a field date_sent. No. of days should be calculated from date_sent to current date. this total no. of days that falls undet 0 to 30 days range should be retrieved. How do I do it. I have the below code but it's not working. no errors but does not filter properly.

query = query.Where(x => x.DATE_SENT != null);

query = query.Where(x => (int)(EntityFunctions.DiffDays(currentDate, (DateTime)x.DATE_SENT)) >= 0 &&
(int)(EntityFunctions.DiffDays(currentDate, (DateTime)x.DATE_SENT)) <= 30);

Any guidance on how it can be handled will be great.

mww mww

You can get it using this query.

var dtDiff = DateTime.Now.AddDays(-30);
query  = query.Where(z=>  z.DATE_SENT >= dtDiff);

31-60 days

   var dtDiff31 =DateTime.Now.AddDays(-31);
   var dtDiff60 =DateTime.Now.AddDays(-60);
   query  = query.Where(z=>  z.DATE_SENT >= dtDiff60 && z.DATE_SENT <= dtDiff31 );