Getting started with LINQ to Entities is relatively easy :-)
Calling ObjectContext.CreateQuery<T> gives you back a ObjectQuery<T>. ObjectQuery<T> is IQueryable<T> and so we can use it for LINQ queries.
That means that using a plain old Vanilla ObjectContext we can write queries such as;
using (ObjectContext ctx = new ObjectContext("Name=NorthwindEntities3"))
{
var query = from s in ctx.CreateQuery<Shipper>("NorthwindEntities3.Shippers")
where s.CompanyName == "Speedy Express"
select s;
foreach (Shipper s in query)
{
System.Console.WriteLine(s.ShipperID);
}
}
Or, given that if I've used the tooling to produce a derived NorthwindContext or similar which has a property called Shippers on it then I can shorten that down to;
using (NorthwindContext ctx = new NorthwindContext())
{
var query = from s in ctx.Shippers
where s.CompanyName == "Speedy Express"
select s;
foreach (Shipper s in query)
{
System.Console.WriteLine(s.ShipperID);
}
}
and so begins a journey off into LINQ.
Posted
Wed, Sep 5 2007 1:11 AM
by
mtaulty