Databases Reference
In-Depth Information
var query = from c in context.Customers
join s in context.Suppliers on c.City equals s.City
select new {
c.City,
Customer = c.CompanyName,
Supplier = s.CompanyName
};
foreach (var item in query)
Console.WriteLine(item.City + “ “ + item.Customer + “ “ +
item.Supplier);
The example just displayed uses the join keyword, which mimics the JOIN operator intro-
duced by the SQL-92 standard. You can also use syntax that resembles the “old-style” joins
as well:
var query = from c in context.Customers
from s in context.Suppliers
where c.City == s.City
select new {
c.City,
Customer = c.CompanyName,
Supplier = s.CompanyName
};
Composing Queries
An additional benefit of having LINQ queries compiled in LINQ expressions is that they
can be further modified before execution. This is often beneficial when constructing a
query based on options specified by the user.
string city = “London”;
bool sortByContactName = true;
var query = from c in context.Customers select c;
if (city != null)
query = from c in query where c.City == city select c;
if (sortByContactName)
query = from c in query orderby c.ContactName select c;
else
query = from c in query orderby c.CompanyName select c;
foreach(Customer c in query)
Console.WriteLine(c.CompanyName + “ “ + c.ContactName);
 
Search WWH ::




Custom Search