C#表达式转换方便使用==还是equals运营商和外部参数 [英] C# Expressions easy conversion with == or Equals operator and external parameter

查看:91
本文介绍了C#表达式转换方便使用==还是equals运营商和外部参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有两个参数:



参数1,需要一个T和从中获取了相关产品的标识表达

 表达式来; Func键< T,INT>> prodIdProperty = X => x.Product.Id 



参数2的productid与比较,让我们说

  INT的productid = 5; 

有什么办法,我可以运行时改变这个(代码)进入这样一个新的表达方式:

 表达式来; Func键< T,BOOL>> prodIdProperty = X => x.Product.Id == 5 



基本上,我需要是这样的:

 表达式来; Func键< T,BOOL>> TransformToPredicate(表达式来; Func键< T,INT>> prodIdProperty,诠释产品ID){
//插入表情变换魔术我似乎没有掌握...
}

我需要这个,因为我希望把它作为一个谓语我的EF Where子句,这意味着我不能包括我prodIdProperty的编译版本因为它需要调用的invoke()至极,不支持



(我已经试过的这个问题,但我觉得我过于复杂那儿)


< DIV CLASS =h2_lin>解决方案

这听起来像你想要的东西,如:

 表达式来; FUNC< T,BOOL>> TransformToPredicate(
表达式来; Func键< T,INT>> prodIdProperty,
INT的productId)
{
表达等于= Expression.Equal(prodIdProperty.Body,
表达.Constant(productId参数));
返回Expression.Lambda<&Func键LT; T,BOOL>>(等于,prodIdProperty.Parameters);
}



显示在用行动表明示例代码:

 使用系统;使用System.Linq.Expressions 
;

类测试
{
静态表达式来; Func键< T,BOOL>> TransformToPredicate< T>(
表达式来; Func键< T,INT>> prodIdProperty,
INT的productId)
{
表达等于= Expression.Equal(prodIdProperty.Body,
Expression.Constant(productId参数));
返回Expression.Lambda<&Func键LT; T,BOOL>>(等于,prodIdProperty.Parameters);
}

静态无效的主要()
{
表达式来; Func键<字符串,整数>>长度= X => x.Length;

变种谓词= TransformToPredicate(长度,5);
变种编译= predicate.Compile();

Console.WriteLine(编译(你好)); //真
Console.WriteLine(编译(富)); //假
}
}


Let's say i had two parameters:

parameter 1 an expression that takes a T and fetches the associated product's ID from it

Expression<Func<T,int>> prodIdProperty = x=>x.Product.Id

parameter 2 a productid to compare with , let's say

int productid = 5;

Is there any way I could runtime transform this (in code) into a new expression like this:

Expression<Func<T,bool>> prodIdProperty = x=>x.Product.Id == 5

I basically need something like this:

Expression<Func<T,bool>> TransformToPredicate(Expression<Func<T,int>> prodIdProperty,int productid){
//insert expression transform magic I don't seem to grasp...
}

I need this because I want to pass it as a predicate to my EF Where clause, that means I cannot included Compiled versions of my prodIdProperty as It requires calling Invoke() wich is not supported

(I already tried formulating my problem another way in this question, but I think I overcomplicated it there)

解决方案

It sounds like you want something like:

Expression<Func<T, bool>> TransformToPredicate(
    Expression<Func<T, int>> prodIdProperty,
    int productId)
{
    Expression equals = Expression.Equal(prodIdProperty.Body,
                                         Expression.Constant(productId));
    return Expression.Lambda<Func<T, bool>>(equals, prodIdProperty.Parameters);
}

Sample code showing it in action:

using System;
using System.Linq.Expressions;

class Test
{    
    static Expression<Func<T, bool>> TransformToPredicate<T>(
        Expression<Func<T, int>> prodIdProperty,
        int productId)
    {
        Expression equals = Expression.Equal(prodIdProperty.Body,
                                             Expression.Constant(productId));
        return Expression.Lambda<Func<T, bool>>(equals, prodIdProperty.Parameters);
    }

    static void Main()
    {
        Expression<Func<string, int>> length = x => x.Length;

        var predicate = TransformToPredicate(length, 5);
        var compiled = predicate.Compile();

        Console.WriteLine(compiled("Hello")); // True
        Console.WriteLine(compiled("Foo")); // False
    }
}

这篇关于C#表达式转换方便使用==还是equals运营商和外部参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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