john Cogdle
john Cogdle

Reputation: 1533

Return last 10 days data by date using Entity Framework

I want to return last 10 days list using Entity Framework by date. DateSigned is my date column. I already tried the code shown below, but this does not return the last 10 days of data, this returns 10 days back data. How can I fix it?

var Chart = dbcontext.CampaignEmails
                     .Where(x => x.DateSigned > DateTime.Now.AddDays(-10))
                     .ToList();

Upvotes: 2

Views: 1256

Answers (1)

greyhame
greyhame

Reputation: 274

var tenDaysAgo = DateTime.Today.AddDays(-10);
var Chart = dbcontext.CampaignEmails.Where(x => x.DateSigned >= tenDaysAgo).ToList();

Is what you are looking for i guess. If you only want 10 records you can use Take() LINQ method before the ToList() call. Furthermore, you may need to order your results before even accessing them with a OrderBy().

Upvotes: 3

Related Questions