以下是我的代码,以获取Expression<Func<T,bool>>,这有助于提供Func<T,bool>用于列表过滤,现在,我需要按如下方式创建MemberExpression(在代码中):

 MemberExpression memberExpressionColumn = Expression.Property(parameterType,"X");


其中X是用于过滤的属性的名称,因此当我将其应用于List<Person>时,很容易用属性X替换Name,但是当我使用List<string>作为源时,然后如何创建MemberExpression,这导致异常

void Main()
{
    List<Person> personList = new List<Person>()
        {
            new Person{ Name = "Shekhar", Age = 31},
            new Person{ Name = "Sandip", Age = 32},
            new Person{ Name = "Pramod", Age = 32},
            new Person{ Name = "Kunal", Age = 33}
        };


    var personNameList = personList.Select(p => p.Name).ToList();

    var personNameHashset = new HashSet<string>(personNameList);

    var nameList = new List<string>() { "Kunal", "Pramod", "Mrinal" };

    var finalExpression = personNameHashset.EqualExpression<string>("Name");

    var finalFunc = finalExpression.Compile();

    var result = nameList.Where(finalFunc);

    result.Dump();
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static class ExpressionTreesExtension
{
    public static Expression<Func<T, bool>> EqualExpression<T>(this HashSet<string> nameHashSet, string columnName)
    {
        var expressionList = new List<Expression>();

        ParameterExpression parameterType = Expression.Parameter(typeof(T), "obj");

        // Exception Here - How to create MemberExpression for primitive type list List<string>

        MemberExpression memberExpressionColumn = Expression.Property(parameterType,columnName);

        var containsMethodInfo = typeof(StringListExtensions).GetMethod("Contains", new[] { typeof(string), typeof(HashSet<string>) });

        ConstantExpression constant = Expression.Constant(nameHashSet, typeof(HashSet<string>));

        var resultExpression = Expression.Call(null, containsMethodInfo, memberExpressionColumn, constant);

        return Expression.Lambda<Func<T, bool>>(resultExpression, parameterType);
    }
}

public static class StringListExtensions
{
    /// <summary>
    /// String Extension - Contains (Substring)
    /// </summary>
    /// <param name="source"></param>
    /// <param name="subString"></param>
    /// <returns></returns>
    public static bool Contains(this string name, HashSet<string> nameHashSet)
    {
        return nameHashSet.Contains(name);
    }
}

最佳答案

据我了解,您需要获得如下结果:

var result = nameList.Where(obj => personNameHashset.Contains(obj));


由于不需要从字符串中获取Name属性,因此不应该使用memberExpressionColumn,并且可以使用containsMethodInfo调用ParameterExpression parameterType。您可以简化方法来获得Expression<Func<T, bool>>像这样

public static Expression<Func<T, bool>> EqualExpression<T>(this HashSet<string> nameHashSet)
{
    ParameterExpression parameterType = Expression.Parameter(typeof(T), "obj");
    var containsMethodInfo = typeof(StringListExtensions).GetMethod("Contains", new[] { typeof(string), typeof(HashSet<string>) });
    ConstantExpression constant = Expression.Constant(nameHashSet, typeof(HashSet<string>));
    var resultExpression = Expression.Call(null, containsMethodInfo, parameterType, constant);
    return Expression.Lambda<Func<T, bool>>(resultExpression, parameterType);
}


并称之为:

var finalExpression = personNameHashset.EqualExpression<string>();
var finalFunc = finalExpression.Compile();
var result = nameList.Where(finalFunc);

关于c# - 如何为List <string>和Expression <Func <T,bool >>创建MemberExpression,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52641636/

10-10 07:43