【问题标题】:Can I Compare the day name with the date Entity Framework 6我可以将日期名称与日期实体框架 6 进行比较吗
【发布时间】:2016-03-22 05:51:31
【问题描述】:

我正在开发新闻功能。新闻模型包含发布日期...... 有没有办法根据发布日期的日期名称从数据库中过滤我的记录,例如在控制器操作中:

var data1 = db.News.Where(x => x.PublishingDate >= DateTime.Now
    && x.PublishingDate.Day == (int)DayOfWeek.Sunday);
ViewBag.SundayNews = data1;

或者如果有其他方法或任何参考。

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-5 entity-framework-6 asp.net-mvc-5.1


    【解决方案1】:

    试试这个解决方案:http://c-sharp-snippets.blogspot.ru/2011/12/getting-dayofweek-in-linq-to-entities.html

    var firstSunday = new DateTime(1753, 1, 7); 
    var filtered = from e in dbContext.Entities
                   where EntityFunctions.DiffDays(firstSunday, e.SomeDate) % 7 == (int)DayOfWeek.Monday 
                   select e;
    

    firstSunday 存储 MS SQL DATETIME 类型的最小值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多