1

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();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
john Cogdle
  • 1,423
  • 2
  • 16
  • 26
  • Possible duplicate of [How to use DATEADD over column in LINQ - DateAdd is not recognized by LINQ](https://stackoverflow.com/questions/17371798/how-to-use-dateadd-over-column-in-linq-dateadd-is-not-recognized-by-linq) – rekiem87 Jun 29 '18 at 17:17
  • he's not saying he has a runtime error...its not a duplicate – Ctznkane525 Jun 29 '18 at 17:18
  • Can u answer how can i implement solution on my case? – john Cogdle Jun 29 '18 at 17:18
  • Are you sure it isn't working? It should work. Maybe you need to save the calculated date in a variable first, but then you would get an exception anyway... – Progman Jun 29 '18 at 17:20
  • This returns me 10 days back data only not 10 days to today date – john Cogdle Jun 29 '18 at 17:21
  • @johnCogdle can you let us know what value does the DateTime.Now.AddDays(-10) return? I suspect you either have incorrect system date or some timezon thing is causing it not to work. – Jamshaid K. Jun 29 '18 at 17:21
  • @johnCogdle for "10 days to today's date" You must include `>=` sign. – Jamshaid K. Jun 29 '18 at 17:27

1 Answers1

2
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().

greyhame
  • 264
  • 2
  • 13