LastName = "Smails", FirstName = "Spaulding",
Nationality = "Irish"
},
new Employee {
LastName = "Ivanov", FirstName = "Ivan",
Nationality = "Russian"
}
};
var query = from emp in employees
group emp by new {
Nationality = emp.Nationality,
LastName = emp.LastName
};
foreach( var group in query ) {
Console.WriteLine( group.Key );
foreach( var employee in group ) {
Console.WriteLine( employee.FirstName );
}
Console.WriteLine();
}
}
}
Notice the anonymous type within the group clause. What this says is that I want to partition
the input collection into groups where both the Nationality and LastName are the same. In this
example, every group ends up having one entity except one, and it’s the one where Nationality is
Russian and LastName is Ivanov. Essentially how it works ...