C#C
C#3y 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();

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)
Was this page helpful?