Monthly Archives: June 2010
Intersect operator in C# LINQ
The Intersect SQO returns a sequence of elements that the first and second sequences contain; its method signature is the same as Union’s except for the method name. var query19 = (from p in ProductList where p.CategoryID == 1 || … Continue reading
Union operator in C# LINQ
The Union SQO combines two sequences of the same structure into one without regard to matching values. var query18 = (from c in CustomerList join s in SupplierList on c.City equals s.City select new { c.City, c.CompanyName, Type = “Customer” … Continue reading
Distinct Set Operator in C# LINQ
The Distinct SQO removes elements with duplicate values from a sequence. The following example returns a list of unique country names from the CustomerList collection’s Country field. var query17 = (from c in CustomerList orderby c.Country select c.Country).Distinct(); foreach … Continue reading
Example of GroupBy with Query Expression Syntax in C# LINQ
var query16 = from c in CustomerList orderby c.Country, c.CustomerID group c by c.Country into g select new { Country = g.Key, Group = g }; foreach (var g in query16) { sbResult.Append(g.Group + “\r\n”); foreach (var c in … Continue reading
Example of GroupBy with Method Call Syntax in C# LINQ
var query15 = CustomerList.GroupBy(c = > c.Country); query15 = CustomerList.OrderBy(c = > c.Country) .ThenBy(c = > c.CustomerID) .GroupBy(c = > c.Country); foreach (var g in query15) { sbResult.Append(g.Key + “\r\n”; foreach (var c in g) sbResult.Append(String.Format(“ CustomerID = … Continue reading
OrderBy using CurrentCultureIgnoreCase in C# LINQ
var query14 = ProductList.Where(p = > p.Category.CategoryName == “Beverages”) .OrderBy(p = > p.ProductName, StringComparer.CurrentCultureIgnoreCase); foreach (var p in query14) sbResult.Append(String.Format(“ProductID = {0}, UnitPrice = {1:c}, “ & “Name = {2}\r\n”, p.ProductID, p.UnitPrice, p.ProductName));
ThenBy and ThenByDescending example in LINQ
var query13 = ProductList.OrderBy(p = > p.CategoryID) .ThenByDescending(p = > p.UnitPrice) .ThenBy(p = > p.ProductName); query13 = from p in ProductList orderby p.CategoryID, p.UnitPrice descending, p.ProductName select p; foreach (var p in query13) sbResult.Append(String.Format(“CategoryID = {0}, UnitPrice = … Continue reading
Concat operator example in C# LINQ
var Query12 = CustomerList.Where(c = > c.Country == “USA”) .Concat(CustomerList.Where(c = > c.Country == “Canada”) .Concat(CustomerList.Where(c = > c.Country == “Mexico”))); Query12 = (from c in CustomerList where c.Country == “USA” select c) .Concat((from c in CustomerList where c.Country … Continue reading
Examples of a left outer join in LINQ
The following examples implement a left outer join of Customer with Order objects with default values supplied to display Customer objects that don ’ t have corresponding Order objects: Order emptyOrder = new Order { OrderID = 0, CustomerID = … Continue reading
Concatenation Operator Example in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var Query12 = CustomerList.Where(c => c.Country == "USA") .Concat(CustomerList.Where(c => c.Country == "Canada") .Concat(CustomerList.Where(c => c.Country == "Mexico"))); Query12 = (from c in CustomerList where c.Country == "USA" select c) .Concat((from … Continue reading
Nested Left Outer Inner Join Example in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); // Define default order instance Order emptyOrder = new Order { OrderID = 0, CustomerID = "XXXXX", EmployeeID = 0, OrderDate = DateTime.Parse("1/1/1900"), RequiredDate = DateTime.Parse("1/1/1900"), ShipVia = 0, Freight = 0M, … Continue reading
Simple Flat Left Inner Join Example in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var query10 = CustomerList.Join(OrderList, c => c.CustomerID, o => o.CustomerID, (c, o) => new { c.CustomerID, c.CompanyName, o.OrderID, o.OrderDate}) .Take(10); query10 = (from c in CustomerList join o in OrderList … Continue reading
SkipWhile/TakeWhile Example in C# LINQ
var query9 = CustomerList.Select((cust, index) => new { cust, index }) // .OrderBy(c => c.cust.Country) .SkipWhile(c => c.cust.Country != "USA") .TakeWhile(c => c.cust.Country == "USA") .Select(c => new { c.cust.CustomerID, c.cust.CompanyName, c.index }); foreach (var c in query9) sbResult.Append(String.Format("CustomerID … Continue reading
Skip/Take Example in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var query8 = CustomerList.Select((cust, index) => new { cust, index }) .Where(c => c.cust.Country == "USA") .Select(c => new { c.cust.CustomerID, c.cust.CompanyName, c.index }) .Skip(10) .Take(2); query8 = (from c in … Continue reading
SelectMany Translation for an Equi-join Expression in C# LINQ
var query7 = from c in CustomerList from o in OrderList where c.Country == "USA" && o.CustomerID == c.CustomerID select new { o, c }; //Translates to: query7 = from c in CustomerList.SelectMany(o => OrderList, (c, o) => new … Continue reading
Simple SelectMany Example with Associated Collection in C# LINQ
The SelectMany operator implements cross and outer joins, and flattens sequences into elements that resemble rows of relational views based on joins.SelectMany returns from the source object a many – to – one sequence having the set of properties specified … Continue reading
Multiple Select Projection with Index Values in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var query5 = CustomerList.Select((cust, index) => new {cust, index}) .Where(c => c.cust.Country == "USA" && c.index > 70) .Select(c => new { c.cust.CustomerID, c.cust.CompanyName, c.index }); // Demonstrates use of the Let … Continue reading
Simple Select Projection in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var query4 = CustomerList.Where(c => c.Country == "USA") .Select(c => new { c.CustomerID, c.CompanyName, c.Country }); query4 = from c in CustomerList where c.Country == "USA" select new {c.CustomerID, c.CompanyName, c.Country}; … Continue reading
Where Method Call with Index Criterion in C# LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var query3 = CustomerList.Where((c, index) => c.Country == "USA" && index > 70); // Apply IndexOf() method, which achieves the same result and supports query expressions query3 = CustomerList.Where(c => c.Country … Continue reading
Compound Where Expression in LINQ
public List<Customer> CustomerList; StringBuilder sbResult = new StringBuilder(); var query2 = CustomerList.Where(c => c.Country == "USA" && c.Orders.Any()); query2 = from c in CustomerList where c.Country == "USA" && c.Orders.Any() select c; foreach (var c in query2) { sbResult.Append(String.Format("CustomerID … Continue reading