LINQ Cheat Sheet
LINQ Cheat Sheet
var col = from o in orders select new { OrderID = o.OrderID, Cost = o.Cost }; var col2 = orders.Select (o => new { OrderID = o.OrderID, Cost = o.Cost } );
Gruppierung
var orderCounts = from o in orders group o by o.CustomerID into g select new { CustomerID = g.Key, TotalOrders = g.Count() }; var orderCounts = orders.GroupBy (o => o.CustomerID) .Select(g => new { CustomerID = g.Key, TotalOrders = g.Count() });
Filterung
var col = from o in Orders where o.CustomerID == 84 select o; var col2 = Orders.Where (o => o.CustomerID == 84);
Sortierung
var col = from o in orders orderby o.Cost ascending select o; var col2 = orders.OrderBy (o => o.Cost); var col3 = from o in orders orderby o.Cost descending select o; var col4 = orders.OrderByDescending (o => o.Cost); var col5 = from o in orders orderby o.CustomerID, o.Cost descending select o; // Gleiches Ergebnis wie zuvor var col6 = from o in orders orderby o.Cost descending orderby o.CustomerID select o; // Reihenfolge der orderby beachten! var col7 = orders.OrderBy (o => o.CustomerID) .ThenByDescending (o => o.Cost);
Join
var col = from c in customers join o in orders on c.CustomerID equals o.CustomerID select new { c.CustomerID, c.Name, o.OrderID, o.Cost }; var col2 = customers.Join(orders, c => c.CustomerID, o => o.CustomerID, (c, o) => new { c.CustomerID, c.Name, o.OrderID, o.Cost } );
Elementoperationen
// Exception, falls keine Elemente var cust = (from c in customers where c.CustomerID == 84 select c).Single(); var cust1 = customers.Single (c => c.CustomerID == 84); // null, falls keine Elemente var cust = (from c in customers where c.CustomerID == 84 select c) .SingleOrDefault(); var cust1 = customers.SingleOrDefault (c => c.CustomerID == 84); // Neue Customer-Instanz in Sequenz, // falls keine Elemente var cust = (from c in customers where c.CustomerID == 85 select c).DefaultIfEmpty (new Customer()).Single(); var cust1 = customers.Where (c => c.CustomerID == 85) .DefaultIfEmpty (new Customer()).Single(); // Exception, falls keine Elemente var cust = (from o in orders where o.CustomerID == 84 orderby o.Cost select o).First(); // Last() und ElementAt() genauso var cust1 = orders.Where (o => o.CustomerID == 84) .OrderBy(o => o.Cost) .First();
// 0, falls keine Elemente var i = (from c in customers where c.CustomerID == 85 select c.CustomerID) .FirstOrDefault(); var j = customers.Where (c => c.CustomerID == 85) .Select(o => o.CustomerID) .FirstOrDefault();
Konvertierung (Export)
ToArray
string[] names = (from c in customers select c.Name).ToArray();
ToDictionary
Dictionary<string, double> customerOrdersWithMaxCost = (from oc in (from o in orders join c in customers on o.CustomerID equals c.CustomerID select new { c.Name, o.Cost }) group oc by oc.Name into g select g) .ToDictionary(g => g.Key, g => g.Max(oc => oc.Cost)); Dictionary<int, Customer> col = customers.ToDictionary (c => c.CustomerID);
Konvertierung (Import)
OfType
object[] data = { 0, 1, "two", 3.0 }; // Ignoriert Elemente, die kein int sind var v = from d in data.OfType<int>() where d < 2 select d; var books = shoppingCart.Articles.OfType<Book>();
Cast
object[] data = { "Bill", "Ted", "Maria" }; // Exception, falls ein Element kein String var names = from d in data.Cast<string>() where d.Length > 3 select d; var controls = this.Controls.Cast<Control>();
ToList
List<Order> ordersOver10 = (from o in orders where o.Cost > 10 orderby o.Cost).ToList();
ToLookup
ILookup<int, string> customerLookup = customers.ToLookup (c => c.CustomerID, c => c.Name);