bojeff30
asked on
using linq in C# to get DateTime range from list
I am using linq(in c#) to get the records going back 1 year back. My code below works if i use .AddMonths(-10); but if I used AddMonths(-12); all the records are returned. I'm not sure why this occurs.
Could someone tell how can i get the previous 12 month using linq.
var result = items
.Where(x => !x.IsDeleted)
.OrderByDescending(x => x.OpenedAt)
.Take(perPage)
.ToList();
var startDate1 = DateTime.Now.AddMonths(-10 );
var newResult1 = result.Where(x => x.CreatedAt >= startDate1);
var startDate2 = DateTime.Now.AddMonths(-12 );
var newResult2 = result.Where(x => x.CreatedAt >= startDate2);
Could someone tell how can i get the previous 12 month using linq.
var result = items
.Where(x => !x.IsDeleted)
.OrderByDescending(x => x.OpenedAt)
.Take(perPage)
.ToList();
var startDate1 = DateTime.Now.AddMonths(-10
var newResult1 = result.Where(x => x.CreatedAt >= startDate1);
var startDate2 = DateTime.Now.AddMonths(-12
var newResult2 = result.Where(x => x.CreatedAt >= startDate2);
What are the values of x.CreatedAt that are not evaluating correctly, can you post some examples?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
issue due to bad data