C
C#2y ago
İrşat

✅ (Linq) Assigning value in OrderBy lambda function

int hrVoteResult = 0;
comment.highlightedReply = await _mapper.ProjectTo<ReplyDtoRead_2>(_db.Replies
.Where(r => r.deletedStatus.body == "Default" &&
r.commentId == comment.id)
.OrderByDescending(r =>
{
hrVoteResult = r.Votes.Sum(v => v.body == true ? 1 : -1;
return hrVoteResult;
})
.Take(1))
.FirstOrDefaultAsync();
int hrVoteResult = 0;
comment.highlightedReply = await _mapper.ProjectTo<ReplyDtoRead_2>(_db.Replies
.Where(r => r.deletedStatus.body == "Default" &&
r.commentId == comment.id)
.OrderByDescending(r =>
{
hrVoteResult = r.Votes.Sum(v => v.body == true ? 1 : -1;
return hrVoteResult;
})
.Take(1))
.FirstOrDefaultAsync();
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
Angius
Angius2y ago
Then do it the way it works ¯\_(ツ)_/¯ The issue is, as it says, expression trees need the lambdas to be expression-bodied
İrşat
İrşatOP2y ago
I need that vote result 😄
Angius
Angius2y ago
.Select() it in a way that also gives it to you
İrşat
İrşatOP2y ago
Alternatively, I have to assign it to "comment.highlightedReply.voteResult
Angius
Angius2y ago
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()
İrşat
İrşatOP2y ago
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?
Angius
Angius2y ago
It wants an expression Because the framework knows how to translate expressions to SQL
İrşat
İrşatOP2y ago
What kind of expression are we talking here? sql query kind of thing?
Angius
Angius2y ago
No, an expression, in the C# sense Something that immediately returns a single value Like, expression-bodied methods
İrşat
İrşatOP2y ago
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 ?
Angius
Angius2y ago
In both cases it's C# code It's just that an expression can easily be parsed and translated And a statement body cannot
İrşat
İrşatOP2y ago
.OrderByDescending(r =>
{
return r.Votes.Sum(v => v.body == true ? 1 : -1);
})
.OrderByDescending(r =>
{
return r.Votes.Sum(v => v.body == true ? 1 : -1);
})
Why doesn't this work then? Isn't this just the same code with extra steps?
Angius
Angius2y ago
But it's not an expression It has a body, denoted by {} And a return
İrşat
İrşatOP2y ago
AI insists that it should work. It has a long way to go it seems
Angius
Angius2y ago
AI is notorious for being confidently incorrect
İrşat
İrşatOP2y ago
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.
comment.highlightedReply = await _mapper.ProjectTo<ReplyDtoRead_2>(_db.Replies
.Where(r => r.deletedStatus.body == "Default" &&
r.commentId == comment.id)
.OrderByDescending(r => r.Votes.Sum(v => v.body == true ? 1 : -1))
.Take(1))
.FirstOrDefaultAsync();
if(comment.highlightedReply != null){
comment.highlightedReply.voteResult = _db.Votes
.Where(v => v.targetReplyId == comment.highlightedReply.id)
.Sum(v => v.body == true ? 1 : -1);
}
comment.highlightedReply = await _mapper.ProjectTo<ReplyDtoRead_2>(_db.Replies
.Where(r => r.deletedStatus.body == "Default" &&
r.commentId == comment.id)
.OrderByDescending(r => r.Votes.Sum(v => v.body == true ? 1 : -1))
.Take(1))
.FirstOrDefaultAsync();
if(comment.highlightedReply != null){
comment.highlightedReply.voteResult = _db.Votes
.Where(v => v.targetReplyId == comment.highlightedReply.id)
.Sum(v => v.body == true ? 1 : -1);
}
This should work. I don't know how else if not expression body
Saber
Saber2y ago
This would work in normal linq, but EF has entirely different rules.
İrşat
İrşatOP2y ago
it pains me to make an another query 😄 (I am eating dinner for the next 20 min, sorry if I can't reply)
Want results from more Discord servers?
Add your server