Essential C# LINQ Queries  Suggest an edit

GroupBy one or multiple fields

Lets assume we have some Film model:

public class Film {
    public string Title { get; set; }
    public string Category { get; set; }
    public int Year { get; set; }
}

Group by Category property:

foreach (var grp in films.GroupBy(f => f.Category)) {
    var groupCategory = grp.Key;
    var numberOfFilmsInCategory = grp.Count();
}

Group by Category and Year:

foreach (var grp in films.GroupBy(f => new { Category = f.Category, Year = f.Year })) {
    var groupCategory = grp.Key.Category;
    var groupYear = grp.Key.Year;
    var numberOfFilmsInCategory = grp.Count();
}

Table Of Contents
22 Enum
25 GUID
65 LINQ Queries
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close