Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4: Selecting Single Record

Tags:

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!

like image 382
SeToY Avatar asked Jan 26 '12 09:01

SeToY


People also ask

How do I select a single value in Entity Framework?

Using Select.Single and SingleOrDefault.: var c = from c in db. table where c.id== id select new { Name = c.Name, }; var query = (from c in db. table where c.id== "Classic id" select new { c.

How do I get specific columns in Entity Framework?

In Entity Framework Core we can select specific columns by using either anonymous types or DTOs.


1 Answers

var address = Context.Addresses.First(a => a.Id == addressId);
like image 122
Ray Avatar answered Sep 19 '22 17:09

Ray