microsoft / referencesource

Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework
https://referencesource.microsoft.com/
MIT License
3.13k stars 1.26k forks source link

I didnt understand how it works #156

Closed caglarsarikaya closed 2 years ago

caglarsarikaya commented 2 years ago

https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/System.Core/System/Linq/Enumerable.cs#L768

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 2 years ago

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.

caglarsarikaya commented 2 years ago

Thanks for your advice, unfortunately I banned on stackoverflow I can asking questions probably you didnt surprised :)

I am trying to understand how group by command is working

we reach an entity like users, putting this users into a queryable object, than group by usernames

linq is doing that easly, but how it works. Because if you reach same table on database you cant grouping by usernames, you have to use some aggregate functions with group by

how linq is grouping this list by some field

I was trying to find an answer for this,

thanks again, I will try better

Best Regards, Caglar

Petr Onderka @.***>, 18 Eyl 2021 Cmt, 14:23 tarihinde şunu yazdı:

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 https://stackoverflow.com/ or on the dotnet/runtime repo https://github.com/dotnet/runtime. 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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/microsoft/referencesource/issues/156#issuecomment-922261868, or unsubscribe https://github.com/notifications/unsubscribe-auth/APBLY2DIWAWNBKUB2R2C6UTUCRZDHANCNFSM5EAL5E5Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.