本文介绍了C#表达式使用==或Equals操作符和外部参数轻松转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个参数:



参数1一个表达式,需要一个T并从中获取关联产品的ID

 表达式< Func< T,int>> prodIdProperty = x => x.Product.Id 

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

  int productid = 5; 

有没有什么办法可以将这个(代码)转换成一个新的表达式,如下所示: / p>

 表达式< Func< T,bool>> prodIdProperty = x => x.Product.Id == 5 

我基本上需要这样的东西:

 表达< Func< T,bool>> TransformToPredicate(表达式< Func< T,int>> prodIdProperty,int productid){
//插入表达式转换魔术我似乎没有把握...
}

我需要这个,因为我想把它作为一个谓词传递给我的EF Where子句,这意味着我不能包含我的prodIdProperty的编译版本因为它需要调用Invoke(),它不支持



(我已经尝试在问题,但我认为我在那里过度复述)

解决方案

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

  Func< T,bool>> TransformToPredicate(
Expression< Func< T,int>> prodIdProperty,
int productId)
{
表达式equals = Expression.Equal(prodIdProperty.Body,
表达式.Constant(的productId));
返回Expression.Lambda< Func< T,bool>>(等于,prodIdProperty.Parameters);
}

示例代码显示在行动中:

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

class Test
{
static Expression< Func< T,bool>> TransformToPredicate< T>(
表达式< Func< T,int>> prodIdProperty,
int productId)
{
表达式equals = Expression.Equal(prodIdProperty.Body,
Expression.Constant(productId));
返回Expression.Lambda< Func< T,bool>>(等于,prodIdProperty.Parameters);
}

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

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

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


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操作符和外部参数轻松转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:57