I prepared this Linq-to-SQL query and it needs to be executed as IQueryable
but it's failing.
When I convert filteredResult
by calling ToList
, then it works well but I need to use filteredResult
as IQueryable
and get select result as below. But I get an error
A lambda expression with a statement body cannot be converted to an expression tree
Went through link below, but, how the below query can be converted to IQueryable
. As per the link, do I need to write up Func<object,object>
or any example to convert query would really helpful.
"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
};
});