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

Mike's Badges

Follow on Twitter
View mike's profile on slideshare
Add to Technorati Favorites
CW Blog Awards

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
(C) Mike Taulty, 2009. All rights reserved. The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems