How to declare a Linq Expression variable in order to have it processed as a dbParameter

前端 未结 1 1362
日久生厌
日久生厌 2021-01-04 22:22

I\'m trying to create dynamic queries against Entity framework (or other Linq provider).

Let me explain my problem with some sample code.

If I hardcode an ex

相关标签:
1条回答
  • 2021-01-04 22:45

    You'll need to create a lambda at compile time to close over the id variable, to which you can then grab the body of an use in the composition of your more complex lambda:

    var id = 12345;
    ParameterExpression param = Expression.Parameter(typeof(ItemSearch), "s");
    Expression prop = Expression.Property(param, "Id");
    Expression<Func<int>> idLambda = () => id;
    Expression searchExpr = Expression.Equal(prop, idLambda.Body);
    
    Expression<Func<ItemSearch, bool>> myLambda =
        Expression.Lambda<Func<ItemSearch, bool>>(searchExpr, param);
    
    0 讨论(0)
提交回复
热议问题