C# LINQ Where in collection 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/973916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:36:31  来源:igfitidea点击:

LINQ Where in collection clause

c#linq-to-sql

提问by Jan W.

I've been looking on google but not finding anything that does the trick for me.

我一直在寻找谷歌,但没有找到任何对我有用的东西。

as you know SQL has a "where x in (1,2,3)" clause which allows you to check against multiple values. I'm using linq but I can't seem to find a piece of syntax that does the same as the above statement.

如您所知,SQL 有一个“where x in (1,2,3)”子句,它允许您检查多个值。我正在使用 linq,但似乎找不到与上述语句相同的语法。

I have a collection of category id's (List) against which I would like to check

我有一个类别 ID(列表)的集合,我想根据这些集合进行检查

I found something that uses the .contains method but it doesn't even build.

我发现了一些使用 .contains 方法的东西,但它甚至没有构建。

采纳答案by CMS

You have to use the Contains method on your id list:

您必须在您的 id 列表中使用 Contains 方法:

var query = from t in db.Table
            where idList.Contains(t.Id)
            select t;

回答by Darin Dimitrov

Here's an articleillustrating the approach. You should indeed use the Contains method over your collection which will be translated into INclause.

这是一篇说明该方法的文章。您确实应该在您的集合上使用 Contains 方法,该方法将被转换为IN子句。

回答by David Hall

The syntax is below:

语法如下:

IEnumerable<int> categoryIds = yourListOfIds;

var categories = _dataContext.Categories.Where(c => categoryIds.Contains(c.CategoryId));

The key thing to note is that you do the contains on your list of ids - not on the object you would apply the in to if you were writing sql.

需要注意的关键是,您在 id 列表中执行 contains - 如果您正在编写 sql,则不在将应用 in 的对象上。

回答by Fragment

Here is my realization of WhereIn() Method, to filter IQueryable collection by a set of selected entities:

这是我对 WhereIn() 方法的实现,通过一组选定的实体过滤 IQueryable 集合:

 public static IQueryable<T> WhereIn<T,TProp>(this IQueryable<T> source, Expression<Func<T,TProp>> memberExpr, IEnumerable<TProp> values) where T : class
    {
        Expression predicate = null;
        ParameterExpression param = Expression.Parameter(typeof(T), "t");

        bool IsFirst = true;

        MemberExpression me = (MemberExpression) memberExpr.Body;
        foreach (TProp val in values)
        {
            ConstantExpression ce = Expression.Constant(val);


            Expression comparison = Expression.Equal(me, ce);

            if (IsFirst)
            {
                predicate = comparison;
                IsFirst = false;
            }
            else
            {
                predicate = Expression.Or(predicate, comparison);
            }
        }

        return predicate != null
            ? source.Where(Expression.Lambda<Func<T, bool>>(predicate, param)).AsQueryable<T>()
            : source;
    }

And calling of this method looks like:

这个方法的调用看起来像:

IQueryable<Product> q = context.Products.ToList();

var SelectedProducts = new List<Product>
{
  new Product{Id=23},
  new Product{Id=56}
};
...
// Collecting set of product id's    
var selectedProductsIds = SelectedProducts.Select(p => p.Id).ToList();

// Filtering products
q = q.WhereIn(c => c.Product.Id, selectedProductsIds);