© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
34 replies
İ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)
.OrderByDescending(r =>  r.Votes.Sum(v => v.body == true ? 1 : -1)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

LINQ Lambda-Expression Diff
C#CC# / help
4y ago
Assigning Lambda to Delegate (PurposesReason)
C#CC# / help
4y ago
❔ Anonymous function in LINQ Select
C#CC# / help
3y ago
✅ Anonymous and lambda function in C#
C#CC# / help
13mo ago