c# - 如何在 LINQ to entities 中使用 Date 函数?

标签 c# linq entity-framework linq-to-entities

我的方法应该返回一个用户注释列表,所以我这样做了:

  var saturday = DayOfWeek.Saturday;
  var query = from note in userNotes
  where note.NoteDate > lastMonth && note.NoteDate.DayOfWeek != saturday
        select note;

但是我得到这个错误:

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

关于如何使用 linq 比较星期几的任何想法?

最佳答案

使用SqlFunctions.DatePart静态方法。它将转化为DATEPART TSQL 函数调用。

var saturday = (int)DayOfWeek.Saturday;
var query = from note in userNotes
            where note.NoteDate > lastMonth && SqlFunctions.DatePart("dw", note.NoteDate) != saturday
            select note;

关于c# - 如何在 LINQ to entities 中使用 Date 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18857972/

相关文章:

c# - 无法在 C# 中隐式转换类型 'System.Collections.Generic.List<AnonymousType#1>

c# - 如何将 c# 代码转换为 vb.net (Linq)?

c# - EF - 另一个一对多内的一对多关系(Facebook 帖子/ react )

c# - 未调用 UITableviewCell GetCell

c# - 如何将表格数据绑定(bind)到下拉列表?

c# - Sql server - 以只读权限执行存储过程

.net - LINQ 到 SQL。级联删除模拟

asp.net - 覆盖实体的删除

entity-framework - 如何在没有 HttpContext 的情况下获取 Entity Framework 种子方法中的文件路径(从包管理器命令执行)

c# - 代码隐藏中的 WPF C# 按钮单击事件