LINQ to SQL的多对多INT ID阵列条件查询 [英] LINQ to SQL many to many int ID array criteria query

查看:162
本文介绍了LINQ to SQL的多对多INT ID阵列条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这应该是很简单,但我做在我这里头,看了所有关于这方面的文章,并尝试了各种东西,但没有运气。

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.

我有一个经典的多对许多设置3个表格。

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

ITEMS
项目编号
说明

ITEMS ItemID Description

ITEMFEATURES
项目编号
FeatureID和

ITEMFEATURES ItemID FeatureID

特点
FeatureID和
说明

FEATURES FeatureID Description

现在我有一个搜索界面,您可以选择任意数量的功能(复选框)。
我让他们均能为int []所谓SearchFeatures。

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.

我只是想找到其中有包含在SearchFeatures特色的项目。

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

例如。是这样的:

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

在我的项目部分类我添加了一个自定义的方法特点(),它只是简单地返回该项目的所有特性,但我仍似乎无法集成在任何可用的方式进入主LINQ查询。

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.

格儿,它得是简单,在SQL这样的1秒的任务。非常感谢。

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

推荐答案

下面的查询将返回项目的基础名单上的列表 searchFeatures

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;

这里的窍门是先从 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;

这篇关于LINQ to SQL的多对多INT ID阵列条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆