簡體   English   中英

如何構建與通用對象進行比較的Linq表達式樹?

[英]How do I build a Linq Expression Tree that compares against a generic object?

我有一個IQueryable和一個類型為T的對象。

我想做IQueryable()。其中​​(o => o.GetProperty(fieldName)== objectOfTypeT.GetProperty(fieldName))

所以......

public IQueryable<T> DoWork<T>(string fieldName)
        where T : EntityObject
{
   ...
   T objectOfTypeT = ...;
   ....
   return SomeIQueryable<T>().Where(o => o.GetProperty(fieldName) == objectOfTypeT.GetProperty(fieldName));
}

Fyi,GetProperty不是一個有效的功能。 我需要一些執行此功能的東西。

我周五下午腦部融化還是這是一件很復雜的事情?


objectOfTypeT我可以做以下......

var matchToValue = Expression.Lambda(ParameterExpression
.Property(ParameterExpression.Constant(item), "CustomerKey"))
.Compile().DynamicInvoke();

哪個工作完美,現在我只需要第二部分:

return SomeIQueryable()。Where(o => o.GetProperty(fieldName) == matchValue);

像這樣:

    var param = Expression.Parameter(typeof(T), "o");
    var fixedItem = Expression.Constant(objectOfTypeT, typeof(T));
    var body = Expression.Equal(
        Expression.PropertyOrField(param, fieldName),
        Expression.PropertyOrField(fixedItem, fieldName));
    var lambda = Expression.Lambda<Func<T,bool>>(body,param);
    return source.Where(lambda);

我已經開始了一個博客,這將涉及一系列的表達主題, 在這里

如果你遇到任何問題,另一個選擇是首先從objectOfTypeT提取值(使用反射),然后在Expression.Constant使用該值,但我懷疑它會“按原樣”。

關於什么:

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

    }

    public Func<T, TRes> GetPropertyFunc<T, TRes>(string propertyName)
    {
        // get the propertyinfo of that property.
        PropertyInfo propInfo = typeof(T).GetProperty(propertyName);

        // reference the propertyinfo to get the value directly.
        return (obj) => { return (TRes)propInfo.GetValue(obj, null); };
    }

    public void Run()
    {
        List<Person> personList = new List<Person>();

        // fill with some data
        personList.Add(new Person { Name = "John", Age = 45 });
        personList.Add(new Person { Name = "Michael", Age = 31 });
        personList.Add(new Person { Name = "Rose", Age = 63 });

        // create a lookup functions  (should be executed ones)
        Func<Person, string> GetNameValue = GetPropertyFunc<Person, string>("Name");
        Func<Person, int> GetAgeValue = GetPropertyFunc<Person, int>("Age");


        // filter the list on name
        IEnumerable<Person> filteredOnName = personList.Where(item => GetNameValue(item) == "Michael");
        // filter the list on age > 35
        IEnumerable<Person> filteredOnAge = personList.Where(item => GetAgeValue(item) > 35);
    }

這是一種在不使用動態查詢的情況下通過字符串獲取屬性值的方法。 缺點是al值將被裝箱/取消裝箱。

從目前為止我所能看到的,它必須是......

IQueryable<T>().Where(t => 
MemberExpression.Property(MemberExpression.Constant(t), fieldName) == 
ParameterExpression.Property(ParameterExpression.Constant(item), fieldName));

雖然我可以得到這個來編譯它並不是完全按照它的要求執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM