GetWeekOfYear实体框架 [英] GetWeekOfYear with Entity Framework

查看:122
本文介绍了GetWeekOfYear实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为日期表已经列,我需要比较这个日期的 WeekOfTheYear DateTime.Now WeekOfTheYear

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

如果我给这样的,

var cal = CultureInfo.CurrentCulture.Calendar;

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

我收到 26 在这里。用同样的方法,在我的实体框架,我需要比较这一周的数据,对于这个我试过喜欢,

I am getting 26 here. The same way, in my Entity Framework, I need to compare this week's data, for this I tried like,

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

当我运行这样的查询,我得到的错误一样,

when I run the query like this, am getting error like,

LINQ到实体无​​法识别方法的Int32 GetWeekOfYear
  (System.DateTime的,System.Globalization.CalendarWeekRule,System.DayOfWeek)
  法,这种方法不能被翻译成店前pression。

"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 a store expression."

我怎样才能获取数据每周在这里,任何一个可以帮我在这里....在此先感谢

How can I fetch the data for weekly basis here, can any one help me out here....thanks in advance

推荐答案

呼叫.ToList()第一。像这样的:

Call .ToList() first. Like this:

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

请参阅this张贴重复的问题。基本上,数据需要在内存中使用GetWeekOfYear功能前。

See this post for duplicate issue. Basically, the data needs to be in memory before using your GetWeekOfYear functions.

正如在评论所指出的,这并带动整个WorkingDays表装入存储器并因此提取比从DB需要更多的数据。有时候,这更是preferable使用存储过程,有时没有,这取决于数据和其它因素的基础上你的应用/数据库体系结构的数量。

As noted in the comments, this does bring the whole "WorkingDays" table into memory and therefore fetching more data than needed from the DB. Sometimes this is more preferable to using Stored Procedures and sometimes not, depending on the amount of data and other factors based on your application/database architecture.

这篇关于GetWeekOfYear实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆