How to write joins query in LINQ?

SheoNarayan
Posted by SheoNarayan under LINQ category on | Points: 40 | Views : 2575
Assuming we have Categories and CategoryParents table in the data source, the joins query is written like below

 var records = from c in db.Categories
join
cp in db.CategoryParents
on c.CategoryParentID equals cp.AutoID
select new
{
c.Active,
c.AutoID,
c.Category1,
c.CategoryParentID,
cp.CategoryParent1,
c.Description
};


In my case the Categories CategoryParentId is the foreign key and CategoryParents AutoID is the primary key.

Thanks

Comments or Responses

Posted by: Gsudhesh on: 8/2/2011 Level:Starter | Status: [Member] | Points: 10
also we can perform the join operation without using the join keyword.

var records = from c in db.Categories
from cp in db.CategoryParents
where c.CategoryParentID == cp.AutoID

select new
{ c.Active,
c.AutoID,
c.Category1,
c.CategoryParentID,
cp.CategoryParent1,
c.Description
};
Posted by: Akiii on: 8/2/2011 Level:Bronze | Status: [Member] | Points: 10
Is it good to use anonymous types while querying data ?

Akiii

Login to post response