Tuesday, 3 September 2013

LINQ BASICS

Linq(Language Integrated Query) is a component released within the .NET 3.5 Framework. It serves the purpose of querying objects. LINQ comprises a series of operators, which are used to query, filter and project data in arrays, enumerable classes, relational databases and XML. LINQ has its own Query Processing Engine. The information returned by a LINQ query is a collection of in-memory object which may be enumerated. For writing code in C# that understands the LINQ syntax, the System.Linq namespace needs to be used.

Syntax of LINQ:
---------------------------------------------------------------------
var numQuery = from num in numbers
where (num % 2) == 0
select num;
---------------------------------------------------------------------

Where:
  • The 'from' keyword logically loops through the contents of the collection (numbers in the above example).
  • The expression with the 'where' keyword is evaluated for each object in the collection.
  • The 'select' statement selects the evaluated object to add to the list being returned.
  • The 'var' keyword is for variable declaration. Since the exact type of the returned object is not known, it indicates that the information will be inferred dynamically.

Operators

While writing queries, you will find a number of operators that we can use. The most general operators are :
  • Projection operators  (Select)
  • Restriction operators (Where)
  • Partitioning operators (Take / Skip, TakeWhile/ SkipWhile)
  • Join operators  (Join)
  • Concatenation operator (Concat)
  • Ordering operators  (order by)
  • Grouping operators  (group by into)
Some of the basic operations that can be performed are:

Filtering:
Filtering causes the query to return only those elements for which the expression is true. The result is produced by using the where clause. In the following example, only those customers who have an address in Mumbai are returned.
---------------------------------------------------------------------
var query = from cust in customers
where cust.City == "Mumbai"
select cust;
---------------------------------------------------------------------

Ordering:
Orderby clause will cause the elements in the returned sequence to be sorted. For example, the following query can be extended to sort the results based on the Name property.
---------------------------------------------------------------------
var query = from cust in customers
where cust.City == "Mumbai" orderby cust.Name ascending
select cust;
---------------------------------------------------------------------

Grouping:
The group clause enables you to group your results based on a key that you specify.
---------------------------------------------------------------------
var query = from i in _itemList
group i by i.Category into g
select new { Category = g.Key, Items = g };
---------------------------------------------------------------------

Joining:
Let say we want to find all the customers and distributors who have the same location. We can write a join query using LINQ as mentioned below:
---------------------------------------------------------------------
var query = from cust in customers
join dist in distributors on cust.City equals dist.City
select new{cust.Name,dist.Name };
---------------------------------------------------------------------

Here, we are using select new clause in place of select because with the new keyword we are building anonymous types with only those two fields. Let’s say customers and distributors have 20 columns combined together and we need only two that is when we use select new.

About Author:
Saju Verghese works with Systems Plus Pvt. Ltd. on DOT NET Technology projects. He can be contacted at saju.v@spluspl.com

No comments:

Post a Comment