xxxxxxxxxx
_bankList.AsEnumerable().GroupBy(d => new { d.CUSTOMERS, Cust = d.CUSTOMERS, Cat = d.CATEGORY })
.Select(g => new { Customer = g.Key.CUSTOMERS, g.Key.Cat }).OrderBy(x => x.Cat).ToList();
xxxxxxxxxx
var groupByCompoundKey =
from student in students
group student by new
{
FirstLetter = student.LastName[0],
IsScoreOver85 = student.ExamScores[0] > 85
} into studentGroup
orderby studentGroup.Key.FirstLetter
select studentGroup;
foreach (var scoreGroup in groupByCompoundKey)
{
string s = scoreGroup.Key.IsScoreOver85 == true ? "more than 85" : "less than 85";
Console.WriteLine($"Name starts with {scoreGroup.Key.FirstLetter} who scored {s}");
foreach (var item in scoreGroup)
{
Console.WriteLine($"\t{item.FirstName} {item.LastName}");
}
}
/* Output:
Name starts with A who scored more than 85
Terry Adams
Name starts with F who scored more than 85
Fadi Fakhouri
Hanying Feng
Name starts with G who scored more than 85
Cesar Garcia
Hugo Garcia
Name starts with G who scored less than 85
Debra Garcia
Name starts with M who scored more than 85
Sven Mortensen
Name starts with O who scored less than 85
Claire O'Donnell
Name starts with O who scored more than 85
Svetlana Omelchenko
Name starts with T who scored less than 85
Lance Tucker
Name starts with T who scored more than 85
Michael Tucker
Name starts with Z who scored more than 85
Eugene Zabokritski
*/
xxxxxxxxxx
var results = persons.GroupBy(
p => p.PersonId,
p => p.car,
(key, g) => new { PersonId = key, Cars = g.ToList() });
xxxxxxxxxx
nums = new int[]{1,1,2,3,4,4,4,4,5};
var groupS =nums.GroupBy(x => x);
// for detail view take look at below or click on source link
// https://leetcode.com/problems/majority-element/discuss/2657752/103-ms-faster-than-97.70-of-C-online-submissions-One-Liner