C
C#17mo ago
Marek

I need help with understanding the flow of LINQ query expressions

Hello, i'm starting to learn the LINQ language and i don't know if i got my understanding of the process of executing right. My "theory" on example below from official Microsoft documentation is this: 1. It splits students into a row of student 2. It groups every student by the Year property into studentGroups 3. It Selects new Anonymous type with Level and HighScore of the group I'm sorry if i messed it up but thats the reason why I'm even creating this post. My theory number two is that everything under from except select and let applies directly to from. I couldn't find which one of these theories is right if even any of them is :D
var queryGroupMax =
from student in students
group student by student.Year into studentGroup
select new
{
Level = studentGroup.Key,
HighestScore = (
from student2 in studentGroup
select student2.ExamScores.Average()
).Max()
};
var queryGroupMax =
from student in students
group student by student.Year into studentGroup
select new
{
Level = studentGroup.Key,
HighestScore = (
from student2 in studentGroup
select student2.ExamScores.Average()
).Max()
};
Thanks for your time, Mark PS: I get what it is doing and i do understand all the keywords just don't know the order of execution
2 Replies
Angius
Angius17mo ago
Basically, yeah Would maybe be easier to understand with the better syntax
var queryGroupMax = students
.GroupBy(student => student.Year)
.Select(g => new {
Level = g.Key,
HighestScore = g.Select(s => s.ExamScores.Average()).Max()
});
var queryGroupMax = students
.GroupBy(student => student.Year)
.Select(g => new {
Level = g.Key,
HighestScore = g.Select(s => s.ExamScores.Average()).Max()
});
And with the knowledge that every LINQ query can be rewritten as one or more loops
Marek
MarekOP17mo ago
Yeah, thanks, that's way easier to understand. :)
Want results from more Discord servers?
Add your server