EntityFramework包含不起作用

时间:2014-02-26 15:58:21

标签: c# entity-framework

departments = (from p in _context.DateSnippets
                               where query.UserInfo.Departments.Contains(p.DepartmentId)
                               group p by p.DepartmentId
                                   into g
                                   select new Tuple<int, DateTime>(g.Key, g.Max(x => x.FromDate))
                               ).ToList();

我为

获得以下例外
  

包含()方法

这是例外

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll 
but was not handled in user code
Additional information: Only parameterless constructors and 
initializers are supported in LINQ to Entities. 

2 个答案:

答案 0 :(得分:4)

问题在于:

new Tuple<int, DateTime>(g.Key, g.Max(x => x.FromDate)

不幸的是,当您对数据库执行此操作时,它将无法工作,因为您无法在构造函数中传递参数。许多变通办法之一可能是使用对象

new MyData {Key = g.Key, Date = g.Max(x => x.FromDate)}

答案 1 :(得分:0)

Contains更改为Any

where query.UserInfo.Departments.Any(d => d.DepartmentId == p.DepartmentId)

将选择更改为投射到烦人的课程或poco。该错误明确声明您不能在构造函数

中包含参数
 select new 
 {
  g.Key,
  g.Max(x => x.FromDate)
 }