汎用オブジェクトと比較するLinq Expression Treeを構築するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/232171

質問

IQueryableとT型のオブジェクトがあります。

IQueryable()。Where(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();

完全に機能しますが、今は2番目の部分だけが必要です:

Return SomeIQueryable()。Where(o =&gt; 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 で使用しますが、 「現状のまま」で問題ないことを疑います。

他のヒント

これまでのところ私が見ることができるものから、それは次のようなものでなければならないでしょう...

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

これをコンパイルすることはできますが、必要な方法で実行されません。

概要:

    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);
    }

これは、動的クエリを使用せずに文字列でプロパティの値を取得する方法です。欠点は、すべての値がボックス化/ボックス化解除されることです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top