我在下面的查询中收到此错误。我搜索了其他类似的链接,但无法使其工作:
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/