如何使用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.


任何帮助将非常感激。

谢谢!

最佳答案

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)
                   };

关于c# - Linq查询如何从今天的日期中选择上一周的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27010273/

10-10 16:22