不在哪里和哪里“或”问题 [英] Where not in and Where "Or" problem

查看:60
本文介绍了不在哪里和哪里“或”问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在这篇文章中为我的问题找到了一半的解决方案:http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/

让我试着解释和说明我的问题。

公共类ClassEntityA 
{
public int Id {get;组; }
public string Text {get;组; }
}

公共类ClassEntityB
{
public int Id {get;组; }
public string Text {get;组; }
public Nullable< ClassEntityA> EntityA {get;组; }
}


//我需要在Ado Entity中执行以下查询。该查询在Linq To Sql中有效,但未在Linq To Entity中支持

list< int> list = new List< in>(){1,2,3};
var itens = DbContext.ClassEntityB.Where(c =>(list.Contains(c.EntityA.Id)== false || c.EntityA == null);

/ /我在Linq To Entity中找到了半解决方案:

//但在这里我可以进行第二次测试(或者c.EntityA == null)。我该怎么做?

list< int> list = new List< in>(){1,2,3};
var itens = DbEntities.ClassEntityB.WhereNotIn(c => c.EntityA。 Id,list.AsEnumerable< int>())


#region不在

私有静态表达式< Func< TElement,bool>>
GetWhereNotInExpression< TElement,TValue>(
Expression< Func< TElement,TValue>> propertySelector,
IEnumerable< TValue> values)
{
ParameterExpression p =
propertySelector.Parameters.Single();

if(!values.Any())
{
return e => true;
}

var unequals = values.Select(value =>
(表达式)Expression.NotEqual(
propertySelector.Body,
Expression.Constant(value,typeof(TValue))

);

var body = unequals.Aggregate< Expression>(
(accumulate,unequal)=> Expression.And(accumulate,unequal));

返回Expression.Lambda< Func< TElement,bool>>(body,p);
}

public static IQueryable< TElement>
WhereNotIn< TElement,TValue>(此IQueryable< TElement>来源,
表达式< Func< TElement,TValue>> propertySelector,
params TValue []值)
{
返回source.Where(GetWhereNotInExpression(
propertySelector,values));
}

public static IQueryable< TElement>
WhereNotIn< TElement,TValue>(此IQueryable< TElement>来源,
表达式< Func< TElement,TValue>> propertySelector,
IEnumerable< TValue>值)
{
返回source.Where(GetWhereNotInExpression(
propertySelector,values));
}

#endregion



你能帮助我吗?

我需要在Ado中进行查询,该查询中包含("不在"或"Property == null"的测试)。

thx。


Luciano Correia

解决方案

任何小贴士? PLZ

Hi guys,

i founf a half solution for my problem in that post: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/

Let me try to explain and illustrate my problem.

public class ClassEntityA
{
    public int Id { get; set; }
    public string Text { get; set; }
}

public class ClassEntityB
{
    public int Id { get; set; }
    public string Text { get; set; }
    public Nullable<ClassEntityA> EntityA { get; set; }
}


// I need to do the following query in Ado Entity. That query works in Linq To Sql, but not suported in Linq To Entity

list<int> list = new List<in>() { 1, 2, 3 };
var itens = DbContext.ClassEntityB.Where(c => (list.Contains(c.EntityA.Id) ==  false || c.EntityA == null);

// i Found a Half Solution to do that in Linq To Entity doing that:

// But Here i can do the second test (OR c.EntityA == null). How can i do That?

list<int> list = new List<in>() { 1, 2, 3 };
var itens = DbEntities.ClassEntityB.WhereNotIn(c => c.EntityA.Id, list.AsEnumerable<int>())


 #region Where Not In

        private static Expression<Func<TElement, bool>>
            GetWhereNotInExpression<TElement, TValue>(
            Expression<Func<TElement, TValue>> propertySelector,
            IEnumerable<TValue> values)
        {
            ParameterExpression p =
                propertySelector.Parameters.Single();

            if (!values.Any())
            {
                return e => true;
            }

            var unequals = values.Select(value =>
                (Expression)Expression.NotEqual(
                    propertySelector.Body,
                    Expression.Constant(value, typeof(TValue))
                )
            );

            var body = unequals.Aggregate<Expression>(
                (accumulate, unequal) => Expression.And(accumulate, unequal));

            return Expression.Lambda<Func<TElement, bool>>(body, p);
        }

        public static IQueryable<TElement>
            WhereNotIn<TElement, TValue>(this IQueryable<TElement> source,
            Expression<Func<TElement, TValue>> propertySelector,
            params TValue[] values)
        {
            return source.Where(GetWhereNotInExpression(
                propertySelector, values));
        }

        public static IQueryable<TElement>
            WhereNotIn<TElement, TValue>(this IQueryable<TElement> source,
            Expression<Func<TElement, TValue>> propertySelector,
            IEnumerable<TValue> values)
        {
            return source.Where(GetWhereNotInExpression(
                propertySelector, values));
        }

        #endregion


Can any of you help me?

i need to do a Query in the Ado that have a ("Where not in" or "Property == null) teste.

thx.


Luciano Correia

解决方案

Any tips guys? plz


这篇关于不在哪里和哪里“或”问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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