

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
LINQ CHEAT SHEET. Query Syntax ... i.e. NULL will be returned if T is a reference type or nullable value type; default(T) will be returned if T is a.
Typology: Lecture notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Lambda Syntax
var col = from o in Orders where o.CustomerID == 84 select o; var col2 = Orders.Where(o => o.CustomerID == 84);
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 } );
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 col9 = from o in orders orderby o.CustomerID, o.Cost descending select o; var col6 = orders.OrderBy(o => o.CustomerID). ThenByDescending(o => o.Cost); //returns same results as above var col5 = from o in orders orderby o.Cost descending orderby o.CustomerID select o; //NOTE the ordering of the orderby’s
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 } );
var OrderCounts = from o in orders group o by o.CustomerID into g select new { CustomerID = g.Key, TotalOrders = g.Count() }; var OrderCounts1 = orders.GroupBy( o => o.CustomerID). Select(g => new { CustomerID = g.Key, TotalOrders = g.Count() });
Lambda Syntax
//select top 3 var col = (from o in orders where o.CustomerID == 84 select o).Take(3); var col2 = orders.Where( o => o.CustomerID == 84 ).Take(3); //skip first 2 and return the 2 after var col3 = (from o in orders where o.CustomerID == 84 orderby o.Cost select o).Skip(2).Take(2); var col3 = (from o in orders where o.CustomerID == 84 orderby o.Cost select o).Skip(2).Take(2);
//throws exception if no elements var cust = (from c in customers where c.CustomerID == 84 select c).Single(); var cust1 = customers.Single( c => c.CustomerID == 84); //returns null if no elements var cust = (from c in customers where c.CustomerID == 84 select c).SingleOrDefault(); var cust1 = customers.SingleOrDefault( c => c.CustomerID == 84); //returns a new customer instance if no elements 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(); //First, Last and ElementAt used in same way var cust4 = (from o in orders where o.CustomerID == 84 orderby o.Cost select o).Last(); var cust5 = orders.Where( o => o.CustomerID == 84). OrderBy(o => o.Cost).Last(); //returns 0 if no elements var i = (from c in customers where c.CustomerID == 85 select c.CustomerID).SingleOrDefault(); var j = customers.Where( c => c.CustomerID == 85). Select(o => o.CustomerID).SingleOrDefault();