// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class Order { public int StudentId { get; set; } public decimal Amount { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } } public class Program { public static void Main(string[] args) { List ordr = new List(); ordr.Add(new Order { StudentId=1, Amount=10.00m }); ordr.Add(new Order { StudentId=2, Amount=20.00m }); ordr.Add(new Order { StudentId=2, Amount=30.00m }); List stud = new List(); stud.Add(new Student { Id=1, Name="A A" }); stud.Add(new Student { Id=2, Name="B B" }); Console.WriteLine("No grouping:"); var q1 = from o in ordr join s in stud on o.StudentId equals s.Id orderby s.Name select new { StudentId=s.Id, Name=s.Name, Amount=o.Amount }; foreach(var r1 in q1) { Console.WriteLine(r1.StudentId + ", " + r1.Name + ", " + r1.Amount); } Console.WriteLine("Grouping:"); var q2 = from o in ordr group o by o.StudentId into g join s in stud on g.Key equals s.Id orderby s.Name select new { StudentId=g.Key, Name=s.Name, Amount=g.Sum(o => o.Amount) }; foreach(var r2 in q2) { Console.WriteLine(r2.StudentId + ", " + r2.Name + ", " + r2.Amount); } } } }