我想选择PersianCalendar
特殊月份的项目。
我用这个亚麻布。
var calendar = new PersianCalendar();
var month = calendar.GetMonth(DateTime.Now);
var referreds= _db.Referreds.Where(m => calendar.GetMonth(m.CreatedDateTime) == month);
但是我得到错误
LINQ to Entities无法识别方法"Int32 GetMonth(System.DateTime)"方法,并且此方法无法转换为存储表达式
如何在linq中选择具有特殊月份的项目??
您不能在LINQ to Entities中直接使用PersianCalendar(除其他原因外,因为底层数据库没有明确支持它)。什么会起作用:
- 使用PersianCalendar.ToDateTime计算一个月的第一天
- 计算下个月的第一天
-
使用这些值查询
_db.Referreds.Where(m=>m.CreatedDateTime>=firstDayOfMonth&&m.CreatedDateTime<firstDayOfNextMonth);
我在这里假设波斯历与公历具有相同的基本属性。如果没有,我相信Jon Skeet会纠正我的错误。