I use c#, linq and EF4.
I have two tables in my DataBase represented in my Conceptual Model:
DataBase Tables:
CmsContents
CmsCategories
CmsRelatedCategories (Pure Juction Table)
Entity Type:
CmsContent
CmsCategory
Entyt Set:
CmsContents
CmsCategories
I have some Navigational Properties:
for CmsContents --> CmsCategories --> Return Collection of Cms CmsCategory
for CmsCategories --> CmsContents --> Return Collection of Cms CmsContents
Data in DataBase for the Junction Table is presented:
CategoryId ContentId
7 1
7 2
9 2
I need query Entity Framwork to retrive all CmsContents which are included in the Junction Table.
At the moment I use this code:
var contents = from cnt in context.CmsContents
where cnt.CmsCategories.Any()
select cnt;
That Returns:
CmsContents
1
2
I need the result showing instead:
CmsContents
1
2
2
I suspect that Any() show me just DISTINCT values, but I need all values listed.
Any idea how to solve it?.
Could you please write me the LINQ query so I can have a clear picture.
Any()doesn’t return values at all – it returns a Boolean value as to whether there are any values in the collection or not. It’s just showing two values because you’re only querying CmsContents, which has two rows, and both rows have at least one category, so both rows are in the result.It looks like you’re really after something like: