c# - 无法创建类型的常量值

标签 c# linq

我在下面的查询中收到此错误。我搜索了其他类似的链接,但无法使其工作:

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.

var users = from a in GetUsers(EmployeeID)
                group new { a } by new { a.EmployeeID } into g
                select new UserMovement { EmployeeID =  g.Key.EmployeeID };


    var resulttst = from s in _repository.GenericRepository<STG_WDUsers>().GetAll()
                  join u in users on s.Employee_ID equals u.EmployeeID
                  where s.EffectiveDate != null && s.EffectiveDate > dtSixMonths
                  group new { s } by new { s.Employee_ID, s.Business_Unit, s.First_Name, s.Last_Name } into g
                  orderby g.Key.Employee_ID, g.Min(m => m.s.EffectiveDate)
                  select new UserMovement
                  {
                      EffectiveDate = g.Min(m => m.s.EffectiveDate),
                      EmployeeID = g.Key.Employee_ID,
                      FirstName = g.Key.First_Name,
                      LastName = g.Key.Last_Name,
                      BusinessUnit = g.Key.Business_Unit,
                      PreviousBU = null
                  };

最佳答案

您收到此错误是因为您在转换为 SQL 的 LINQ 查询中使用本地对象序列 users。不过,UserMovement 对象没有翻译。您可以在 LINQ to SQL 后端中使用本地序列,但前提是它们包含原始值:

var empIds = (from a in GetUsers(EmployeeID)
              select a.EmployeeID).Distinct();
from s in _repository.GenericRepository<STG_WDUsers>().GetAll()
                     .Where(u => empIds.Contains(u.Employee_ID));

关于c# - 无法创建类型的常量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848546/

相关文章:

c# - 为什么我无法在 visual studio 2012 pro 中将用户控件加载到我的工具箱?

c# - 如何按照上次在 asp.net-webpages 中修改的日期顺序对 .GetFiles() 中的字符串数组进行排序?

c# - LINQ 和自然排序顺序

c# - .NET 核心 - 依赖注入(inject)、工厂和 IDisposable

c# - 选中和未选中如何在 C# 中进行转换

c# - 在同一个 Controller ActionResult 中运行我的存储库中的 2 个方法是否正确?

c# - 我如何使用 Entity Framework 获取记录计数匹配谓词

C# 在 LINQ 中定义 LET

c# - 如何引用自身内部结构的实例?

c# - 在 C# 中将数据集发送到没有数据库连接的 Crystal 报表