// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Linq; using System.Collections.Generic; public class LINQforObject { public static void Main(string[] args) { List

lstp = new List

{new P{A = 123, B = "ABC" }, new P{A = 456, B = "DEF" }}; // select en række (af typen P) var q1 = from o in lstp where o.A == 123 select o; foreach(var o in q1) Console.WriteLine(o.A + " " + o.B); // select en enkelt kolonne (af type string) var q2 = from o in lstp where o.A == 123 select o.B; foreach(var o in q2) Console.WriteLine(o); // select dele af en række (af anonym type) var q3 = from o in lstp where o.A == 123 select new { o.A, o.B}; foreach(var o in q3) Console.WriteLine(o.A + " " + o.B); // select med aggregeret funktion til enkelt værdi (af typen double) var q4 = (from o in lstp select o.A).Average(); Console.WriteLine(q4); } }