Geeks With Blogs
Sudheer Kumar ASP.Net, C#, BizTalk, MSBuild, WPF, WCF, WF....
The post assumes that you have basic under standing of LINQ.
The following section tries to give some deeper insight in to the technology.
A lot of examples can be found at 101 Linq Samples (

1.    Language Integrated Query (LINQ):

a.      How does LINQ work?

                                                               i.      When you write an expression for DLINQ, the expression is converted to a set of DB commands. It then opens a connection to get data.

                                                             ii.      When you use LINQ to objects, it iterates through the items in the object and provides you the matches.

                                                            iii.      But the corresponding C# expression gets compiled when you compile the code.

                                                           iv.      So the main advantage is the easiness to loop though a variety of data sources and convenience of having cleaner code.

b. Uses deferred execution of Query expression, which means?

                                                               i.      LINQ executes the expression only when you iterate through the matches

                                                             ii.       LINQ executes the expression every time you iterate through the match collections.


c.       Usage Scenarios for LINQ to Objects:

                                                               i.      You can use standard .Net expression in the “Select” clause. Here you have a List<Employee> for example.

matches = from employee in employees
select employee.FirstName + employee.LastName;

                                                             ii.      You can create anonymous types in select expression.

var emps = from emp in arrEmps

select new {First=emp.FirstName, Last=emp.LastName };

                                foreach(var match in emps)


                                               string strFullName = match.First + match.Last;


Note the usage of implicit types.

                                                            iii.      Can have multiple conditions in the where clause:

IEnumerable<Product> matches;
matches = from product in products
where product.UnitsInStock > 0 && product.UnitPrice > 3.00M

select product;


                                                           iv.      The where clause can even be a method call.

IEnumerable<EmployeeDetails> matches;

matches = from employee in employees

where TestEmployee(employee)

select employee;

                                                             v.      Can Sort using orderby clasue:

IEnumerable<EmployeeDetails> matches;

matches = from employee in employees

orderby employee.LastName, employee.FirstName

select employee;

                                                            vi.      You can use group by:

 var empGrp = from emp in arrEmps

                                                     group emp by emp.Age into g

                                                     select g.Key;          

                foreach (var item in empGrp)


                                                 int j = item;  //Implicit conversion  here


                But if you want to iterate through grouped items:

// Look through all the groups.

foreach (IGrouping<string, EmployeeDetails> group in matches)


// Loop through all the EmployeeDetails objects in the current group.

foreach (EmployeeDetails employee in group)


// Do something with the employee in the group here.




                                                          vii.      Practically, to get an item that can be directly bound to ASP.Net controls: 

var matches = from employee in employees

group employee by employee.TitleOfCourtesy into g

select new {Title = g.Key, Employees = g.Count()};

 Here Count() is an extension method.

Some of the other useful extensions:

var categories = from p in products

group p by p.Category into g

select new {Category = g.Key,

MaximumPrice = g.Max(p => p.UnitPrice),

MinimumPrice = g.Min(p => p.UnitPrice),

AveragePrice = g.Average(p => p.UnitPrice)};


d.      LINQ to DataSet:

                                                               i.      DataSet.Select() has following drawbacks compared to LINQ:

1.       The expression inside Select() of DS is string and hence cannot be validated at compile time.

2.       It can sort and filter, but does not offer as many features as Linq (grouping etc.)

                                                             ii.      Usage Scenarios of LINQ to DataSet:


Un-typed DataSet:

To get a list of Employees whose LastName starts with "D". You are geting Data from the Employee table in DS


IEnumerable<DataRow> matches = from employee

in ds.Tables["Employees"].AsEnumerable()

where employee.Field<string>("LastName").StartsWith("D")

select employee;

                                                To bind this directly to a control:

gridEmployees.DataSource = matches.AsDataView(); // another extension method


Field<String>(“Name”) – As DS does not expose strongly typed data (means data is accessed                 through datarows, columns etc.). This is one way for strongly typed access to DS.

AsEnumerable() :  As Data Table is not Enumerable, another extension method.

Typed DataSet:

var matches = from employee in ds.Employees

where employee.LastName.StartsWith("D")

select new { First = employee.FirstName, employee.LastName };

                                               It is much easier if you are dealing with Typed Datasets.


Next DLINQ...
Posted on Friday, March 28, 2008 10:20 AM .Net 3.0/3.5 | Back to top

Comments on this post: LINQ .. A powerful extension to .Net

# re: LINQ .. A powerful extension to .Net
Requesting Gravatar...
not good
Left by Hirdayanad Mishra on Jan 28, 2009 11:21 PM

# re: LINQ .. A powerful extension to .Net
Requesting Gravatar...
Hi Sudheer kumar,

Good information for linq.

Left by Anil Desai on Mar 16, 2009 1:04 AM

# re: LINQ .. A powerful extension to .Net
Requesting Gravatar...
thx 4 info
Left by het on Jun 21, 2009 3:12 PM

# re: LINQ .. A powerful extension to .Net
Requesting Gravatar...
Just had a call from a mate, who woke up this morning after a heavy night on the booze and had mistaken his laptop for the toilet in the night.
Left by ugg sale on Dec 08, 2010 8:00 PM

# re: LINQ .. A powerful extension to .Net
Requesting Gravatar...
Left by ice maker on Mar 31, 2011 3:51 AM

Your comment:
 (will show your gravatar)

Copyright © Sudheer Kumar | Powered by: