Sunday, December 21, 2008

Sample Linq Entities

This snippet code shows how easy it is to use LINQ to get to your data in SQL-Server. Before you start you need to add a new ADO.NET Entity Data Model to your project where the wizard leads you to select all you need from your project. You can have multiple modles like OrderModle, PurchaseModle and DistributionModle that may overlap some tables but would make development much easier.
In this sample I use an entity modle from my FamilyTree database.

AhouraEntities model = new AhouraEntities();
ObjectQuery people = model.Person;

var panahy = from person in people
where person.Surname.StartsWith("Panah")
select new
{
person.ID,
person.Name
};

grdPeople.DataSource = people;
grdPeople.DataBind();

lstPanahy.DataSource = panahy;
lstPanahy.DataBind();


Obviously you could instanciate the model using the constructor that expects a connectionString.

No comments: