GetWeekOfYear with Entity Framework

I have a column in my table called Date , and I need to compare this date to WeekOfTheYear with DateTime.Now WeekOfTheYear ,

If I give it like this

 var cal = CultureInfo.CurrentCulture.Calendar; int week = cal.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); 

I get 26 here. Similarly, in my Entity Framework, I need to compare data this week, for this I tried,

 entities.WorkingDays.Where(a => cal.GetWeekOfYear(a.DATE,CalendarWeekRule.FirstDay,DayOfWeek.Sunday) == cal.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday) 

when I run such a request, I get an error, for example,

"The LINQ to Entities does not recognize the method 'Int32 GetWeekOfYear (System.DateTime, System.Globalization.CalendarWeekRule, System.DayOfWeek)' method, and this method cannot be translated into the storage expression."

How can I get the data for the weekly base here, can anyone help me here ... thanks in advance

+4
source share
2 answers

First call .ToList (). Like this:

 entities.WorkingDays.ToList().Where(a => cal.GetWeekOfYear(a.DATE,CalendarWeekRule.FirstDay,DayOfWeek.Sunday) == cal.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday) 

See this article for duplication. Basically, the data must be in memory before using GetWeekOfYear functions.

As noted in the comments, this leads to the fact that the entire table "WorkDays" is placed in memory and, therefore, retrieves more data than is required from the database. Sometimes it is preferable to use stored procedures, and sometimes not, depending on the amount of data and other factors based on the architecture of your application / database.

+2
source

You can probably use the day of the year and divide it by 7 in both copies and get enough results?

 Date.DayOfYear / 7 
0
source

All Articles