使用Asp.net Core 3.1时,我从一张表中选择并分组LINQ Expression,但是在使用"any"转入"select"时出现了错误。
然而,在asp.net标准中却能正常工作。
代码:
List<GetObj> liste = dbContext.testTable
.Where(x => x.isActive == true).OrderByDescending(x => x.Id)
.GroupBy(x => new { x.field1, x.field2 })
.Select(x => new GetObj
{
field1 = x.Key.field1,
field2 = x.Key.field2,
totalQuantity = x.Sum(y => y.ldNet),
isMaped = x.Any(y => y.isLastMove == true)
}).ToList();
错误结果如下:
.Any(y => y.isLastMove == True)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
true
和错误信息中的True
大小写不同,这是有什么原因吗? - Peter Smithfrom table in dbContext.testTable where table.isActive == true orderby table.Id descedning group new { table.ldNet, table.isLastMove } by new {table.field1, table.field2} into tableGrp select new GetObj { field1 = x.Key.field1, field2 = x.Key.field2, totalQuantity = tableGrp.Sum(x => x.ldNet), isMaped = tableGrp.Max(x => x.isLastMove) }
如果你要对不同的列使用orderby,你也可以为它设置一个索引。 - Richárd Baldauf