First peek on retrieving Entity Framework entities with Lambda expressions

2009/10/14

After adding an ADO.NET Entity Data Model item to the .Net project, you can easily retrieve a list of entities using Lambda expressions. Let’s suppose that the data model added to the project is called MyDataEntities and that it links to a table called Person, having Name as column. The following code can be used to retrieve the list:

    public static List<Person> GetPersons()
    {
        try
        {
            using (MyDataEntities context = new MyDataEntities())
            {
                return context.Person.OrderBy(p => p.Name).ToList<Person>();
            }
        }
        catch
        {
            // Do exception handling here
            throw;
        }
    }
Advertisements

%d bloggers like this: