LINQ - C# Programming
Get DataSource
In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source (customers) and the range variable (cust).
//QEmp is an IEnumerable
var QEmp = from e in Emp
select e;
var QEmp = from e in Emp
select e;
Filter
Probably the most common query operation is to apply a filter in the form of a Boolean expression. The filter causes the query to return only those elements for which the expression is true. The result is produced by using the where clause. The filter in effect specifies which elements to exclude from the source sequence.
var QEmp = from e in Emp
where e.Country == "India"
select e
----AND Condition----
where e.Country == "India" && e.Name == "Ashvin"
----OR Condition----
where e.Country == "India" || e.Name == "Ashvin" Ordering The orderby clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example, the following query can be extended to sort the results based on the Name property. Because Name is a string, the default comparer performs an alphabetical sort from A to Z. var QEmp = from e in Emp
where e.Country == "India"
orderby e.Name ascending
select e Grouping The group clause enables you to group your results based on a key that you specify. For example you could specify that the results should be grouped by the City.
where e.Country == "India"
select e
----AND Condition----
where e.Country == "India" && e.Name == "Ashvin"
----OR Condition----
where e.Country == "India" || e.Name == "Ashvin" Ordering The orderby clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example, the following query can be extended to sort the results based on the Name property. Because Name is a string, the default comparer performs an alphabetical sort from A to Z. var QEmp = from e in Emp
where e.Country == "India"
orderby e.Name ascending
select e Grouping The group clause enables you to group your results based on a key that you specify. For example you could specify that the results should be grouped by the City.
var QEmp = from e in Emp
group e by e.City;
If you must refer to the results of a group operation, you can use the into keyword to create an identifier that can be queried further. The following query returns only those groups that contain more than two customers:
// custQuery is an IEnumerable>
var QEmp =from e in Emp
group e by e.City into empGroup
where empGroup.Count() > 2
orderby emp.Key
select empGroup;
Join Clause
A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence. The join clause compares the specified keys for equality by using the special equals keyword. The following are three most common join types: var QEmp =from e in Emp
group e by e.City into empGroup
where empGroup.Count() > 2
orderby emp.Key
select empGroup;
· Inner join
· Group join
· Left outer join
Inner Join
The following example shows a simple inner equijoin. This query produces a flat sequence of “product name / category” pairs. The same category string will appear in multiple elements. If an element from categories has no matching products, that category will not appear in the results.
var innerJoinQuery = from category in categories
join prod in products on category.ID
equals prod.CategoryID
select new { ProductName = prod.Name,
Category = category.Name };
|
Group Join
A join clause with an into expression is called a group join.
var innerGroupJoinQuery =
from category in categories
join prod in products on category.ID
equals prod.CategoryID into prodGroup
select new { CategoryName = category.Name,
Products = prodGroup };
You can also, of course, use the result of a group join as the generator of another subquery:
var innerGroupJoinQuery2 =
from category in categories
join prod in products on category.ID
equals prod.CategoryID into prodGroup
from prod2 in prodGroup
where prod2.UnitPrice > 2.50M
select prod2;
Left Outer Join
In a left outer join, all the elements in the left source sequence are returned, even if no matching elements are in the right sequence. To perform a left outer join in LINQ, use the DefaultIfEmpty method in combination with a group join to specify a default right-side element to produce if a left-side element has no matches. You can use null as the default value for any reference type, or you can specify a user-defined default type.
In the following example, a user-defined default type is shown:
var leftOuterJoinQuery =
from category in categories
join prod in products on category.ID
equals prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty (new Product{Name = String.Empty, CategoryID = 0})
select new { CatName = category.Name, ProdName = item.Name };
Comments