3

假设我有一个基类和继承类

public class BaseClass
{
    public String SystemName { get; set; }
    public String UserName { get; set; }
}

internal class InheritedClass : BaseClass
{
    public Guid Id { get; set; }        
}

基类上的某些表达式从外部传递给我的模块(从那里看不到继承的类)

Expression<Func<BaseClass, bool>>

它描述了一些过滤逻辑,例如

Expression<Func<BaseClass, bool>> filter = x => x.SystemName.StartsWith("ABC") && x.UserName != ""

但是在我的模块中我需要使用

Expression<Func<InheritedClass, bool>>

因为我将此过滤器应用于IQueryable,就像那样

IQueryable<InheritedClass> filteredItems = Items.Where(filter);

继承的类具有基类的所有属性 - 所以我不太有经验的头脑假设转换是可能的:)

有没有办法将传递的表达式转换为我需要的?

谢谢你。

4

1 回答 1

0

使用此实用程序类

public static class Utility
{
    //public static IEnumerable<TTarget> Where<TSource, TTarget>(this IEnumerable<TSource> source, Expression<Func<TSource, bool>> predicate) where TTarget : class, TSource
    //{
    //     return source.Where(obj => obj is TTarget).Where(predicate.Compile()).Select(obj => obj as TTarget);
    //}

    public static IQueryable<TTarget> Where<TSource, TTarget>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) where TTarget : class, TSource
    {
        return source.Where(obj => obj is TTarget).Where(predicate).Select(obj => obj as TTarget);
    }
}

例子是这样的,超类和子类就像

class Person
{
    public String Name { get; set; }
    public ushort Age { get; set; }
}

class Student : Person
{
    public String StudentId { get; set; }
}

class Teacher : Person
{
    public String TeacherId { get; set; }
}

使用该实用程序并对其进行测试

            /*List of Persons*/
            List<Person> persons = new List<Person>()
                                                          {
                                                              new Student(){StudentId = "Std-01", Name = "Roy", Age = 24},
                                                              new Student(){StudentId = "Std-02", Name = "Jhon", Age = 25},
                                                              new Teacher(){TeacherId = "Tch-01", Name = "Roy", Age = 24},
                                                              new Teacher(){TeacherId = "Tch-02", Name = "Jhon", Age = 25}
                                                          };
            /*predictions*/
            Expression<Func<Person, bool>> prediction = x => x.Name.Contains("o");


            /*Use the utility*/
                                                                    /*Get Teacher from Persons*/
            IQueryable<Teacher> filteredItems = persons.AsQueryable().Where<Person, Teacher>(prediction);
            List<Teacher> result = filteredItems.ToList();

结果我们会得到 TeacherId="Tch-01" 和 "Tch-02" 的老师

尽管我使用 IEnumerable 而不是使用 IQueryable,但希望它会有所帮助

于 2013-11-04T12:30:09.103 回答