Linq查询DateTime.Date.DayOfWeek

时间:2016-03-30 15:25:58

标签: c# linq datetime

试图只回到1年前的星期四

using (var context = new Context1())
{
      // Get 1 year back only Thursday
      var oneYearEarlier = DateTime.Now.AddYears(-1);

      var query = (from c in context.Daily25Data
                   where c.Date > oneYearEarlier && c.Date.DayOfWeek == DayOfWeek.Thursday
                   select c).ToList();

      Console.WriteLine(query);
}

并获得

  

附加信息:指定类型的成员' DayOfWeek'不是   LINQ to Entities支持。只有初始化者,实体成员和   支持实体导航属性。

2 个答案:

答案 0 :(得分:4)

使用EntityFunctions.DiffDays

This是一个聪明的解决方案

来自他的帖子:

<RNC id="00">
    <abc1>
    </abc1>
    <abc2>
    </abc2>
</RNC>
<RNC id="01" >
    <abc3>
    </abc3>
    <abc4>
    </abc4>
    <abc5>
    </abc5>
    <abc6>
    </abc6>
</RNC>

答案 1 :(得分:0)

LINQ无法识别您的SQL查询的第二部分。您需要中断查询才能执行过滤。

using (var context = new algoventurelab_db1Context())
{
      // Go one year back from current date
      var startDate = DateTime.Today.AddYears(-1);
      // This will get all dates in context greater than start date.
      var query1 = (from c in context.Daily25Data
                   where c.Date >= startDate
                   select c).AsEnumerable();
      //this will filter only thurdays
      var query = from c in query1
                  where c.Date.DayOfWeek == DayOfWeek.Thursday
                  select c).ToList();

      Console.WriteLine(query);
}