实体框架多对多选择

发布于 2024-12-15 04:59:30 字数 437 浏览 2 评论 0原文

该模型包含 2 个具有多对多关系的实体:目录项和关键字

假设定义了 2 个变量:

ObjectQuery<Keyword> KW;
ObjectQuery<CatalogueItem> CI;

KW 包含一些用于选择一组关键字的查询。我需要获取CI来选择至少具有KW中的一个关键字的所有CatalogueItems。

重要的是:不应预先计算或枚举任何内容。关键字枚举需要花费大量时间,但 UI 是基于实时 CatalogueItems 显示的。完美的事情是让CI准备好执行。

The model contains 2 entities with many-to-many relationship: CatalogueItems and Keywords.

Assume that 2 variables are defined:

ObjectQuery<Keyword> KW;
ObjectQuery<CatalogueItem> CI;

KW contains some query for selecting a set of keywords. I need to get CI that selects all CatalogueItems that have at least one Keyword from KW.

An important thing: nothing should be pre-computed or enumerated. Keywords enumeration takes a lot of time, but UI is based on live CatalogueItems displaying. A perfect thing would be to get CI ready for execution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

辞取 2024-12-22 04:59:30
context.CatalogueItems.Where(ci=> ci.Keywords.Where(cik=>KW.Any(cik)));

类似的事情?请检查语法。

context.CatalogueItems.Where(ci=> ci.Keywords.Where(cik=>KW.Any(cik)));

Something like that? Please check the syntax.

空心空情空意 2024-12-22 04:59:30

像这样的东西:

from catalogueItem in CI
from keyword in KW
where catalogueItem.Keywords.Contains(keyword)
select catalogueItem

编辑:
这样就不会每次都计算 KW,请尝试这样做:

var keywords = KW.ToList()
from catalogueItem in CI
from keyword in keywords
where catalogueItem.Keywords.Contains(keyword)
select catalogueItem

否则,我需要查看 KW 中的查询和整个数据库才能知道如何优化查询。

另一种选择是简单地编写一个存储过程并将其与实体框架进行映射。

Something like this:

from catalogueItem in CI
from keyword in KW
where catalogueItem.Keywords.Contains(keyword)
select catalogueItem

EDIT:
So that KW isn't calculated every time, try doing this:

var keywords = KW.ToList()
from catalogueItem in CI
from keyword in keywords
where catalogueItem.Keywords.Contains(keyword)
select catalogueItem

Otherwise, I'll need to see the query in KW and your whole database to be able to know how to optimize the query.

Another option would be to simply write a Stored Procedure and map it with Entity Framework.

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