Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq到实体和存储表达式_C#_Linq_Linq To Entities_Linq Expressions_Predicatebuilder - Fatal编程技术网

C# linq到实体和存储表达式

C# linq到实体和存储表达式,c#,linq,linq-to-entities,linq-expressions,predicatebuilder,C#,Linq,Linq To Entities,Linq Expressions,Predicatebuilder,我从事的项目使用一些动态linq查询来创建实体。 我有大量的案例,为了避免代码重复,我将重构为一个方法。 但使用不在存储表达式中的方法将导致抛出异常。 解决方案之一是将方法结果封装到一个表达式中,该表达式可以由linq到entitie查询进行解释 考虑一下这一准则: parentExpression = x => x.child.Any(y=>IsGoodChild(y,childType, childSize)); private bool IsGoodChild(child c

我从事的项目使用一些动态linq查询来创建实体。 我有大量的案例,为了避免代码重复,我将重构为一个方法。 但使用不在存储表达式中的方法将导致抛出异常。 解决方案之一是将方法结果封装到一个表达式中,该表达式可以由linq到entitie查询进行解释

考虑一下这一准则:

parentExpression = x => x.child.Any(y=>IsGoodChild(y,childType, childSize));

private bool IsGoodChild(child c, int childType, int childSize){
     return c.type == childType && c.size == childSize;
}
“parentExpression”是我的EF的“Parent”类型的谓词。 此代码引发异常,“IsGoodChild”方法返回布尔值,不能由linq解释为实体

所以,我想要这样的东西:

parentExpression = x => x.child.AsQueryable().Any(IsGoodChild(childType, childSize));

private System.Linq.Expression.Expression<Func<child, bool>> IsGoodChild(int childType, int childSize){
     return  ????
}
parentExpression = x => x.child.Any(y=>y.type == childType && y.size == childSize);
我使用了resharper中的extract方法,并按如下方式生成:

private Expression<Func<child,Boolean>> IsGoodChildFunctional(Int32 childType, Int32 childSize)
{
    return c => c.type == childType && c.size == childSize; 
}
私有表达式IsGoodChildFunctional(Int32 childType,Int32 childSize)
{
返回c=>c.type==childType&&c.size==childSize;
}

但是我也有.NET Framework数据提供程序错误1025'error…

在这种情况下,编译器很聪明,给定一个匿名方法,它将根据声明的类型在表达式树或编译的lambda之间切换。以下方面应起作用:

private Expression<Func<child,Boolean>> 
IsGoodChildFunctional(Int32 childType, Int32 childSize)
{
    return c => c.type == childType && c.size == childSize; 
}

创建一个静态泛型方法,该方法将返回一个
表达式
表达式
是使用工厂方法构建的

public static Expression<Func<TTargetObject,Boolean>> IsGoodChildFunctional<TTargetObject>(Int32 childType, Int32 childSize)
{
            var e = Expression.Parameter(typeof(TTargetObject), "e");
            var childTypeMember = Expression.MakeMemberAccess(e, typeof(TTargetObject).GetProperty("childType"));
            var childSizeMember = Expression.MakeMemberAccess(e, typeof(TTargetObject).GetProperty("childSize"));
            var  childTypeConstant = Expression.Constant(childType, childType.GetType());
            var  childSizeConstant = Expression.Constant(childSize, childSize.GetType());
            BinaryExpression b;
            BinaryExpression bBis;
            Expression<Func<TTargetObject, bool>> returnedExpression;
            b = Expression.Equal(childTypeMember , childTypeConstant );
            bBis2 = Expression.Equal(childSizeMember, c2);
            var resultExpression = Expression.AndAlso(b, bBis);
            returnedExpression = Expression.Lambda<Func<TTargetObject, bool>>(resultExpression , e);
            return returnedExpression;
}
公共静态表达式IsGoodChildFunctional(Int32 childType,Int32 childSize)
{
var e=表达式参数(typeof(TTargetObject),“e”);
var childTypeMember=Expression.MakeMemberAccess(e,typeof(TTargetObject).GetProperty(“childType”);
var childSizeMember=Expression.MakeMemberAccess(e,typeof(TTargetObject).GetProperty(“childSize”);
var childTypeConstant=Expression.Constant(childType,childType.GetType());
var childSizeConstant=Expression.Constant(childSize,childSize.GetType());
二元表达b;
二元表达bBis;
表达返回表达;
b=表达式.Equal(childTypeMember,childTypeConstant);
bBis2=表达式.Equal(childSizeMember,c2);
var resultExpression=表达式,并且也是(b,bBis);
returnedExpression=Expression.Lambda(resultExpression,e);
返回表达式;
}
这就是所谓的:

var predicat = IsGoodChildFunctional<child>(childType, childSize);
parentExpression = x => x.child.Any(predicat);
var predict=IsGoodChildFunctional(childType,childSize);
parentExpression=x=>x.child.Any(谓词);

it not work=>.NET Framework数据提供程序错误1025'error它应该是
y=>IsGoodChildFunctional(y.childType,y.childSize)
否则lambda表达式仍然被视为
Func
而不是表达式。另见。
var predicat = IsGoodChildFunctional<child>(childType, childSize);
parentExpression = x => x.child.Any(predicat);