LINQ不接受Contains()

时间:2012-01-22 02:26:52

标签: c# linq entity-framework

  

可能重复:
  'Contains()' workaround using Linq to Entities?

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

我需要使用3个条件从大表中选择行: - “schooljaar”需要是之前设定的价值, - “p_bamatype”需要是不在设置中定义的列表中的值(这是一个StringCollection) - “p_stdgeb”必须是不在设置中定义的列表中的值(这也是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等价物。

2 个答案:

答案 0 :(得分:2)

如果其中一个重复的答案没有帮助,here is a link可能会有一个答案。它引用了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);

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

答案 1 :(得分:0)

替换Contains()的.Any()方法为我工作。我换了:

objListing.Contains(myObject)

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