Characteristics:
- Only works with supportable data source (backed by LINQ provider). LINQ to Objects|SQL|XML|Entities|DataSets.
- Deferred execution.
Imagine the following scenarios:
1. How do you select a customers with country='gb'?
var customers = new List
new Customer(){ID=1, Name="Abba", Country="gb", Yob="1950"},
new Customer(){ID=2, Name="Bubble", Country="gb", Yob="1970"},
new Customer(){ID=3, Name="Chaplin", Country="us", Yob="1940"},
new Customer(){ID=4, Name="Gum", Country="gb", Yob="2009"},
};
Normal way:
foreach(Customer c in customers)
{
if(c.country.ToLover().Equals("gb") {...}
}
LINQ way:
var query = from c in customers
where c.country.ToLower().Equals("gb")
select c;
Verdict:
Nothing special. right?
2. What if you want to sort the results of scenario 1 by name (ASC) followed by year of birth (DESC)?
LINQ:
var query = from c in customers
where c.country.Equals("gb")
orderby c.name ascending, c.yob descending,
select c;
3. Any shortcut to scenario 2. Yes.
The LINQ query could further translate to
var query =
customers.OrderBy(s=> s.name).OrderByDescending(s=>s.yob);
4. How to find distinct word from senteces?
string[] chickenQuotes = {
"
"
}
var query =
(from
where !word.Equals('.')
select word).Distinct();
Home time.. continue later