Self join in Entity Framework

c# entity entity-framework

Question

I want to have following type of query in entity frame work

SELECT  c2.* 
FROM    Category c1 INNER JOIN Category c2
ON      c1.CategoryID = c2.ParentCategoryID
WHERE   c1.ParentCategoryID is NULL

How to do the above work in Entity framework...

1
3
5/22/2010 10:49:44 AM

Accepted Answer

Well, I don't know much about EF, but that looks something like:

var query = from c1 in db.Category
            where c1.ParentCategoryId == null
            join c2 in db.Category on c1.CategoryId equals c2.ParentCategoryId
            select c2;
9
5/22/2010 10:56:16 AM

Popular Answer

Just to tidy this up the following is a bit nicer and does the same thing:

var query = from c1 in db.Category
            from c2 in db.Category
            where c1.ParentCategoryId == null
            where c1.CategoryId == c2.ParentCategoryId
            select c2;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow