LINQ to SQL 多对多 int ID 数组条件查询

发布于 2024-09-10 19:10:40 字数 540 浏览 1 评论 0原文

好吧,这应该很简单,但我在这里绞尽脑汁,阅读了所有关于此的文章并尝试了各种方法,但没有运气。

我有 3 个表,采用经典的多对多设置。

项目 物品ID 描述

项目特点 物品ID 特征ID

特征 特征ID 描述

现在我有一个搜索界面,您可以在其中选择任意数量的功能(复选框)。 我将它们全部作为一个名为 SearchFeatures 的 int[] 很好地获取。

我只是想找到具有 SearchFeatures 中包含的功能的项目。

例如:

return db.Items.Where(x => SearchFeatures.Contains(x.ItemFeatures.AllFeatures().FeatureID))

在我的 Items 部分类中,我添加了一个自定义方法 Features() ,它只是返回该 Item 的所有功能,但我似乎仍然无法以任何可用的方式将其集成到主 LINQ 查询中。

呃,这一定很简单,比如 SQL 中的 1 秒任务。非常感谢。

Ok this should be really simple, but I am doing my head in here and have read all the articles on this and tried a variety of things, but no luck.

I have 3 tables in a classic many-to-many setup.

ITEMS
ItemID
Description

ITEMFEATURES
ItemID
FeatureID

FEATURES
FeatureID
Description

Now I have a search interface where you can select any number of Features (checkboxes).
I get them all nicely as an int[] called SearchFeatures.

I simply want to find the Items which have the Features that are contained in the SearchFeatures.

E.g. something like:

return db.Items.Where(x => SearchFeatures.Contains(x.ItemFeatures.AllFeatures().FeatureID))

Inside my Items partial class I have added a custom method Features() which simply returns all Features for that Item, but I still can't seem to integrate that in any usable way into the main LINQ query.

Grr, it's gotta be simple, such a 1 second task in SQL. Many thanks.

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

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

发布评论

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

评论(1

み青杉依旧 2024-09-17 19:10:40

以下查询将返回基于 searchFeatures 列表的项目列表:

from itemFeature in db.ItemFeatures
where searchFeatures.Contains(itemFeature.FeatureID)
select itemFeature.Item;

这里的技巧是从 ItemFeatures 表开始。


It is possible to search items that have ALL features, as you asked in the comments. The trick here is to dynamically build up the query. See here:

var itemFeatures = db.ItemFeatures;

foreach (var temp in searchFeatures)
{
    // You will need this extra variable. This is C# magic ;-).
    var searchFeature = temp;

    // Wrap the collection with a filter
    itemFeatures =
        from itemFeature in itemFeatures
        where itemFeature.FeatureID == searchFeature
        select itemFeature;
}

var items =
    from itemFeature in itemFeatures
    select itemFeature.Item;

The following query will return the list of items based on the list of searchFeatures:

from itemFeature in db.ItemFeatures
where searchFeatures.Contains(itemFeature.FeatureID)
select itemFeature.Item;

The trick here is to start with the ItemFeatures table.


It is possible to search items that have ALL features, as you asked in the comments. The trick here is to dynamically build up the query. See here:

var itemFeatures = db.ItemFeatures;

foreach (var temp in searchFeatures)
{
    // You will need this extra variable. This is C# magic ;-).
    var searchFeature = temp;

    // Wrap the collection with a filter
    itemFeatures =
        from itemFeature in itemFeatures
        where itemFeature.FeatureID == searchFeature
        select itemFeature;
}

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