.Any() 上的 Linq 和 EF

发布于 2024-10-21 21:06:22 字数 1070 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2024-10-28 21:06:23

Any() 根本不返回值 - 它返回一个布尔值,表示集合中是否有任何值。它只显示两个值,因为您只查询 CmsContents,它有两行,并且两行都至少有一个类别,因此两行都在结果中。

看来你真的在追求类似的东西:

var contents = from cnt in context.CmsContents
               from category in cnt.CmsCategories
               select cnt;

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:

var contents = from cnt in context.CmsContents
               from category in cnt.CmsCategories
               select cnt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文