0

I have been through the below code but dont understand how group clause makes the grouping

Please help in this . I am new to c# .

public static List<Student> GetStudents() { // Use a collection initializer to create the data source. Note that each element // in the list contains an inner sequence of scores. List<Student> students = new List<Student> { new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 72, 81, 60}}, new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}}, new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {99, 89, 91, 95}}, new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {72, 81, 65, 84}}, new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {97, 89, 85, 82}} }; return students; } List<Student> students = GetStudents(); // Write the query. var studentQuery = from student in students let avg = (int)student.Scores.Average() group student by (avg == 0 ? 0 : avg / 10); 

I dont understand how StudentQuery has been generated . Thanks in advance .

2
  • 1
    This is not the matter a new Learner has to care about, I'm pretty sure that many LINQ users just know How to use it even without noticing How it's implemented, learning stuff like this will slow you down. It's similar that When you learn WPF, you always wonder How it renders 3D Visual. Of course it should be cared about BUT it's an advanced topic, not for a Learner at all. Commented Oct 14, 2013 at 9:38
  • Thanks king king . A very good advice i got after a long time . i am from c++ background , i have started c# few days back . can you please tell me which way i should proceed . and i want to go for a microsoft certification too . please help .thanks in adv . Commented Oct 14, 2013 at 9:53

1 Answer 1

2

Grouping refers to the operation of putting data into groups so that the elements in each group share a common attribute.The GroupBy divides students into groups - those with average 0-9, 10-19, 20-29, 30-39, etc. You should look at http://msdn.microsoft.com/en-us//library/bb546139.aspx

p.s. the

 group student by (avg == 0 ? 0 : avg / 10); 

seems to be excessive to me. You can change it to simplier

 group student by (avg / 10); 

pps: I prefer the other style of LINQ, but it's totally a personal choice. The other style would be

var studentQuery = students.GroupBy(x => x.Scores.Average() / 10); 
Sign up to request clarification or add additional context in comments.

1 Comment

Its better to go for lambda . :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.