microsoft/referencesource

I didnt understand how it works

caglarsarikaya opened this issue · 2 comments

public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) {

IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Abram" , Age = 21 } 
    };

var groupedResult = from s in studentList
                    group s by s.Age;

//iterate each group        
foreach (var ageGroup in groupedResult)
{
    Console.WriteLine("Age Group: {0}", ageGroup .Key); //Each group has a key 
             
    foreach(Student s in ageGroup) // Each group has inner collection
        Console.WriteLine("Student Name: {0}", s.StudentName);
}

the output will be

AgeGroup: 18
StudentName: John
StudentName: Bill
AgeGroup: 21
StudentName: Steve
StudentName: Abram
AgeGroup: 20
StudentName: Ram

hi guys,
we cant grouping something like in SQL server with group by age, it wants to use aggregate function with group by function

how linq grouping like that, and what is the created query?

thanks for your help

svick commented

This is not the right place for this kind of question, this repository is only for publishing the source code of .Net Framework.

I'd suggest you ask either on Stack Overflow or on the dotnet/runtime repo. But when you do, make sure to make it more clear what you're asking, ideally including an example, because I did not understand what exactly you're asking.