LINQ not accepting Contains()

本文关键字:Contains accepting not LINQ | 更新日期: 2023-09-27 18:21:07

可能重复:
';Contains()';使用Linq到实体的解决方法?

在C#中使用LINQ时,我遇到了以下问题;

我需要使用3个条件从大表中选择行:-"schooljaar"需要是一个价值观,之前设定,-"p_bamatype"需要是设置中定义的列表中的NOT值(这是StringCollection)-"p_stdgeb"需要是设置中定义的列表中的NOT值(这也是StringCollection)

我有这个代码:

var set = (db.SA_Opleiding.Where(opleiding => opleiding.schooljaar == schooljaar
                                 &&
                                 !Properties.Settings.Default.Admin_Studiegebieden_Exclude.Cast
                                                           <string>().ToList().Contains(
                                                               opleiding.p_stdgeb.ToString())
                                 &&
                                 !Properties.Settings.Default.Admin_Studietypes_Exclude.Cast
                                                           <string>().ToList().Contains(
                                                               opleiding.p_bamatype.ToString()))
                          .Select(opleiding => new OpleidingModel()
                                    {
                                        Id = opleiding.p_opleiding,
                                        LanNames =
                                            new Dictionary
                                            <string, string>()
                                                {
                                                    {
                                                        "NL",
                                                        opleiding.
                                                        opleidingNL
                                                        },
                                                    {
                                                        "FR",
                                                        opleiding.
                                                        opleidingFR
                                                        },
                                                    {
                                                        "EN",
                                                        opleiding.
                                                        opleidingEN
                                                        }
                                                }
                                    }))
                         .ToList<OpleidingModel>();
        return set;

但是,LINQ无法转换Contains方法。我读到其他人也有同样的问题,但我似乎找不到一个像样的解决方案。所描述的问题真的有解决方案吗?所以我真正需要的是一个NOT IN(字符串集合)LINQ等价物。

LINQ not accepting Contains()

如果各种重复答案中有一个没有帮助,这里有一个可能的链接。它引用了Linq对实体的扩展:

public static class QueryExtensions
{
    public static IQueryable<TEntity> WhereIn<TEntity, TValue>
    (
        this ObjectQuery<TEntity> query,
        Expression<Func<TEntity, TValue>> selector,
        IEnumerable<TValue> collection
    )
    {
        ParameterExpression p = selector.Parameters.Single();
        //if there are no elements to the WHERE clause,
        //we want no matches:
        if (!collection.Any()) return query.Where(x=>false);
        if (collection.Count() > 3000) //could move this value to config
            throw new ArgumentException("Collection too large - execution will cause stack overflow", "collection");
        IEnumerable<Expression> equals = collection.Select(value =>
           (Expression)Expression.Equal(selector.Body,
                Expression.Constant(value, typeof(TValue))));
        Expression body = equals.Aggregate((accumulate, equal) =>
            Expression.Or(accumulate, equal));
        return query.Where(Expression.Lambda<Func<TEntity, bool>>(body, p));
    }
}

还有其他几种类似的产品。

编辑

我已经为上面的代码提供了上下文,下面是可能的用法:

db.SA_Opleiding.WhereIn(v => v.SomeCollection);

我从未使用过这种特定的扩展方法,但它们基本上都基于相同的原理。

用.Any()方法代替Contains()对我有效。我替换了:

objListing.Contains(myObject)

带有

objListing.Any(x => x.Object.ID == myObject.ID)