✅ (Linq) Assigning value in OrderBy lambda function
Error: A lambda expression must have an expression body to be converted to an expression tree.
This works fine btw;
.OrderByDescending(r => r.Votes.Sum(v => v.body == true ? 1 : -1)
18 Replies
Then do it the way it works ¯\_(ツ)_/¯
The issue is, as it says, expression trees need the lambdas to be expression-bodied
I need that vote result 😄
.Select()
it in a way that also gives it to youAlternatively, I have to assign it to "comment.highlightedReply.voteResult
Ah, you're already using automapper to map it to some weirdly-named DTO
So you probably can't
.Select()
it
Well, either mess around with the mapper config so you get what you need
Or try to make that .Select()
work with the mapper
Or ditch the mapper here and just .Select()
I can give the vote result straight from the dto constructor but it's definetely bad practice
Btw, why can't I use other codes in lambda function? What does it want as a return?
It wants an expression
Because the framework knows how to translate expressions to SQL
What kind of expression are we talking here?
sql query kind of thing?
No, an expression, in the C# sense
Something that immediately returns a single value
Like, expression-bodied methods
So, it wants something to turn it into sql so it can fetch stuff. And I am trying to run c# code in it which is a no-no ?
In both cases it's C# code
It's just that an expression can easily be parsed and translated
And a statement body cannot
Why doesn't this work then? Isn't this just the same code with extra steps?
But it's not an expression
It has a body, denoted by
{}
And a return
AI insists that it should work. It has a long way to go it seems
AI is notorious for being confidently incorrect
As a last resort, I can make it two different queries. One that gets the highlighted reply. Other one gets the vote result by the id of this reply.
This should work. I don't know how else if not expression body
This would work in normal linq, but EF has entirely different rules.
it pains me to make an another query 😄
(I am eating dinner for the next 20 min, sorry if I can't reply)