Linq - EF : How to query a Junction table?
I use c#, linq and EF4.
I have two tables in my DataBase represented in my Conceptual Model:
DataBase Tables:
CmsContents
CmsRelatedCo开发者_JS百科ntents (Junction table)
Entity Type:
CmsContent
I have some Navigational Properties:
for CmsContent --> CmsContents --> Return Collection of CmsContent --> from role: CmsContent1 to CmsContent (map ToContentId)
for CmsContent --> CmsContents1 --> Return Collection of CmsContent --> from role: CmsContent to CmsContent1 (map FromContentId)
Data in DataBase for the Junction Table (CmsRelatedContents) is presented:
FromContentId ToContentId
4 3
5 2
In table CmsContents:
ContentId
2
3
4
5
I need use Linq or EF to retrieve Objects in CmsContent
associated in the ToContentId
column filtering the Junction table for a specific FromContentId
Do you have an idea how to do it?
Please provide me sample of code. Thanks for your help on this!
EDIT : SOLUTION
// Option A:
var test = from cnt in context.CmsContents
where cnt.CmsContents.Any(t => t.ContentId == contentId)
select cnt;
// Option B:
var toContents = context.CmsContents.Where(r => r.CmsContents.Any(t => t.ContentId == contentId));
Thanks guys from your support!
Not sure if i understood you correctly but this might be what you need:
dbContext.CmsContent.Where(r => r.CmsContents.Any(t => t.Id = someId))
精彩评论