【发布时间】:2020-03-22 14:24:16
【问题描述】:
我准备了这个 Linq-to-SQL 查询,它需要以 IQueryable 执行,但它失败了。
当我通过调用ToList 转换filteredResult 时,它运行良好,但我需要使用filteredResult 作为IQueryable 并获得如下选择结果。但我得到一个错误
带有语句体的 lambda 表达式不能转换为表达式树
通过下面的链接,但是,如何将下面的查询转换为IQueryable。根据链接,我是否需要写 Func<object,object> 或任何转换查询的示例真的很有帮助。
"A lambda expression with a statement body cannot be converted to an expression tree"
var result = filteredResult.Select(g => {
var type1 = g.FirstOrDefault(x => x.CategoryId == (int)CategoryEnum.Typ1);
var type2 = g.FirstOrDefault(x => x.CategoryId == (int)CategoryEnum.Typ2);
return new AutoDetailDto
{
MilestoneId = g.Key.MilestoneId,
MilestoneName = g.Key.MilestoneName,
PGrade = type1?.GDR,
PGradeChange = type1?.HighestGDR
QGrade = type2.GDR,
QGradeChange = type2?.HighestGDR
};
});
【问题讨论】:
标签: c# linq linq-to-sql