.Any() 上的 Linq 和 EF
我使用 C#、linq 和 EF4。
我的数据库中有两个表在我的概念模型中表示:
数据库表:
CmsContents
CmsCategories
CmsRelatedCategories (Pure Juction Table)
实体类型:
CmsContent
CmsCategory
实体集:
CmsContents
CmsCategories
我有一些导航属性:
for CmsContents --> CmsCategories --> Return Collection of Cms CmsCategory
for CmsCategories --> CmsContents --> Return Collection of Cms CmsContents
呈现了连接表的数据库中的数据:
CategoryId ContentId
7 1
7 2
9 2
我需要查询实体框架来检索包含的所有 CmsContents在连接表中。
目前我使用此代码:
var contents = from cnt in context.CmsContents
where cnt.CmsCategories.Any()
select cnt;
返回:
CmsContents
1
2
我需要显示结果:
CmsContents
1
2
2
我怀疑 Any() 仅显示不同的值,但我需要列出所有值。
知道如何解决吗?
您能否给我写一个 LINQ 查询,以便我能有一个清晰的了解。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Any()
根本不返回值 - 它返回一个布尔值,表示集合中是否有任何值。它只显示两个值,因为您只查询 CmsContents,它有两行,并且两行都至少有一个类别,因此两行都在结果中。看来你真的在追求类似的东西:
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: