Entity Framework 4: Selecting Single Record

SeToY picture SeToY · Jan 26, 2012 · Viewed 52.2k times · Source

I'm currently planning on switching my "manual query-writing" code to a nice SQL framework, so I can leave the queries or sql things to the framework, instead of writing the queries myself.

Now I'm wondering how I can get a single record from my table in Entity Framework 4?

I've primarily used SQL like SELECT * FROM {0} WHERE Id = {1}. That doesn't work in EF4, as far as I'm concerned.

Is there a way I can select a single ID-Based record from my Context?

Something like:

public Address GetAddress(int addressId)
{
    var result = from Context.Addresses where Address.Id = addressId;

    Address adr = result as Address;

    return Address;
}

Thank you!

Answer

Ray picture Ray · Jan 26, 2012
var address = Context.Addresses.First(a => a.Id == addressId);