I’m currently have Lambda Expresion: http://pastebin.com/ZGCiQdHe
And i get ArgumentException in 53 line
http://msdn.microsoft.com/en-us/library/bb340145(v=vs.90).aspx
var lExpresion = Expression.Lambda<Func<TEntity, bool>>(body, parametrsNumber, parametrsTyp, parametrsLp);
But i not understand what i do wrong.
I want to get:
WHERE ((twr_gidnumer =1 and twr_gidtyp = 1 and twr_gidlp = 1) OR
(twr_gidnumer =1 and twr_gidtyp = 1 and twr_gidlp = 2))
–ANSWERR FULL
Based on article:
http://blogs.msdn.com/b/mattwar/archive/2007/07/31/linq-building-an-iqueryable-provider-part-ii.aspx
https://stackoverflow.com/Converting 2 argument Lambda expression to 1 argument Lambda expression
public static Expression<Func<CDNXL_TwrKarty, bool>> Bind2nd(Expression<Func<CDNXL_TwrKarty, CDNXL_TwrKarty,CDNXL_TwrKarty, bool>> source)
{
Expression newBody = new Rewriter(source.Parameters[0]).Visit(source.Body);
return Expression.Lambda<Func<CDNXL_TwrKarty, bool>>(newBody, source.Parameters[0]);
}
internal class Rewriter : ExpressionVisitor
{
private readonly ParameterExpression candidate_;
public Rewriter(ParameterExpression candidate)
{
candidate_ = candidate;
}
protected override Expression VisitParameter(ParameterExpression p)
{
return candidate_;
}
}
//REPLACE EXECUTING PLACE
var retMultiLamnda = Expression.Lambda<Func<CDNXL_TwrKarty, CDNXL_TwrKarty, CDNXL_TwrKarty, bool>>(body, parametrsNumber, parametrsTyp, parametrsLp);
var retOneLambda = Bind2nd(retMultiLamnda);
var retQuery = query.Where(retOneLambda);
return retQuery;
Thanx Rafal for Help.
First thing that I see is that your delegate type does not math with your invocation list. You expect to create function that receives one argument of type
TEntityand yet you pass tree parameter expressions to function. Note that you execute this overload of Lablda method.OK, I’ll try to be more clear:
Those generic arguments
ArgTypemust match withparameterExpressionsForArg. There must be equal number of generic types for argumetnts as parameterExpressions for arguments. And types of those must also match.So if you want to have tree
parameterExpressionsfor some reason then you have to have tree arguments in your method:might be correct call in this case obviously it wont match the
Wherecall.If you want to merge tree expressions into one matching
Whereargument then you have to replace all redundantParamteterExpressions.