views:

29

answers:

1

Having some experience with Linq to SQL I'm trying out ADO Entity framework now. In Linq to SQL, I would create a Linq to SQL class, drag my tables across to build the data context. Then I'd instantiate the datacontext class and run some lambda against one of the properties in the datacontext class.

Now, with ADO entity framework I add the Entity Data Model class, and add the tables to the data model. My Entity Data Model class now has a bunch of ObjectQuery<> properties, one for each table I added.

Now what do I do with those properties? How do I call them? Anyone have code examples?

+3  A: 

Sure. I have a long presentation on this.

As a simple answer to your question, here are some things you can do with the ObjectQuery<T> properties.

Return a list of objects:

IEnumerable<Customer> result = Context.Customers;
return result;

Return one object:

return Context.Customers.Where(c => c.Id == someId).First();

Project onto a presentation model:

return (from c in Customers
        where c.Id == someId
        select new CustomerPresentation
        {
            Id = c.Id,
            Name = c.Name,
            OrderCount = c.Orders.Count(),
            PhoneNumbers = from p in c.PhoneNumbers
                           select new PhoneNumberPresentation
                           {
                               AreaCode = p.AreaCode,
                               // etc.
                           },
            // etc.
        }).First();
Craig Stuntz