Linq查询如何从今天的日期选择最后一周的数据

时间:2014-11-19 06:20:25

标签: c# linq asp.net-mvc-4

如何使用LINQ

从当前日期选择周数据(最近7天的数据)

我所尝试的是,我不确定我写的是正确还是错误

var QueryDeatils = from M in db.Tr_Mealcode
                   where M.codeDate.Date >= DateTime.Now.Date.AddDays(-7)
                   group M by M.merchantID into G
                   select new
                   {
                      MerchantId=G.Select(m=>m.merchantID)
                   };

尝试这个后我得到了一个例外

The specified type member 'Date' is not supported in LINQ to Entities. 
Only initializers, entity members, and entity navigation properties are supported.

任何帮助都会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:5)

var dateCriteria = DateTime.Now.Date.AddDays(-7);
var QueryDeatils = from M in db.Tr_Mealcode
                   where M.codeDate >= dateCriteria 
                   group M by M.merchantID into G
                   select new
                   {
                      MerchantId=G.Select(m=>m.merchantID)
                   };