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şat2y ago
I need that vote result 😄
Angius
Angius2y ago
.Select() it in a way that also gives it to you
İrşat
İrşat2y 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şat2y 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şat2y 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şat2y 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şat2y 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şat2y 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şat2y 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şat2y 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
More Posts
troubles with list length```cs class Solution { static void Main(string[] args) { string line = Console.ReadL❔ When does the IDisposable objects gets automatically disposed ?```cs public bool Method() { bool result = false; using(var disposableClass = new DisposableClas❔ i need some help with this please the dash is not workingprivate bool canDash = true; private bool isDashing; private float dashingPower = 24f; p❔ How DI works?Been using dependency injection to share variables between classes a lot lately, and it got me wonde❔ How put specific UserControl to ContentControl baseing on DataContext.In https://www.codeproject.com/articles/1118762/generic-wpf-crud-control-getting-started project we ❔ How can I make an instantiated object move?I'm trying to make an object spawned with the instantiate function move, but I can't get it to work.❔ How to input a reference of a class into a function with an argument of an interface` I want to save the result of whatever StartFight does to fighter1 and fighter 2, specifically the ❔ File IO Question.Is there anyway to empty a json file? I know you can delete the file but is there anyway to empty al✅ Need assistance compiling an older dotnet appI'm attempting to compile the latest version of the "Loki" character editor Valheim for my kids and❔ Websocket connection fails, however a javascript websocket can connect to server fineI am using `System.Net.WebSockets` to create a websocket client to a websocket server I am running.