Mike Taulty's Blog
Bits and Bytes from Microsoft UK
ADO.NET Entity Framework - LINQ. Getting Started.

Blogs

Mike Taulty's Blog

Elsewhere

Archives

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

Comments

Jason Haley wrote Interesting Finds: September 5, 2007
on Wed, Sep 5 2007 7:27 AM
Christopher Steen wrote Link Listing - September 5, 2007
on Wed, Sep 5 2007 8:46 PM
Link Listing - September 5, 2007
Sam Gentile wrote New and Notable 188
on Fri, Sep 7 2007 6:51 AM
BizTalk Server One of the things that I deal with fairly often in the financial instutions that are doing