2017-01-24 5 views
1

MongoDriverを使用してMongoDBのasp.netコアにlinq式を生成しようとしています。式ジェネレータで二進数のランタイム値にアクセスします。前もって感謝します!ダイナミックなlinq式を構築しているときにDictonary <string、object>の値にアクセスできないようです

private Expression<Func<Dictionary<string, object>, bool>> GenerateWhereExpression(Dictionary<string, object> filterParams) 
{ 
    var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x"); 
    var dictPropery = Expression.PropertyOrField(pe, "name"); // Dictonary value with respect to the name key 
    var methodCall = Expression.Call(dictPropery, typeof(string).GetMethod("Contains"), Expression.Constant(filterParams["name"], typeof(string))); 
    var lambda = Expression.Lambda<Func<Dictionary<string, object>, bool>>(methodCall, pe); 
    return lambda; 
} 

私が達成しようとしているのは、レポートのすべてのデータを取得するクエリのwhere句です。

方法の結果として作成されなければならないExpresssiomは、次のようになります。私は私でそれをテストしている

private Expression<Func<Dictionary<string, object>, bool>> 
        GenerateWhereExpression(Dictionary<string, object> filterParams) 
{ 
    var pe = Expression.Parameter(typeof(Dictionary<string, object>), "x"); 
    // it is call of x.getItem("name") what is the same as x["name"] 
    var dictPropery = Expression.Call(pe, 
      typeof(Dictionary<string, object>).GetMethod("get_Item"), 
      Expression.Constant("name")); 

    //cast to ((string)x.getItem("name")) 
    var castProperty = Expression.Convert(dictPropery, typeof(string)); 
    var methodCall = Expression.Call(castProperty, 
        typeof(string).GetMethod("Contains"), 
        Expression.Constant(filterParams["name"], typeof(string))); 
    var lambda = Expression.Lambda<Func<Dictionary<string, object>, bool>>(methodCall, pe); 
    return lambda; 
} 

x => ((string)x["name"]).Contains(term) 
+1

'.Call'は' string'の 'MethodInfo'を使用しています。 –

+0

x.Keys [0] .Contains( "a") ' – Rafal

+0

のように生成することを目指すプレーンなC#コードを書いてください。私はそれを理解していない、それはどのようにmongodbクエリのために使用できるかわからない –

答えて

1

[OK]を、あなたはあなたの方法を変更するには、この方法が必要ですそれはうまくいくようです。 実際に、辞書から項目を取得するには、Itemプロパティ(Building Expression Tree Using a Parameter's Indexer)に電話する必要があります。しかし、いずれか私は間違った方法を使用しているか、またはMongoDbドライバは正しく翻訳することができませんでしたので、私はそれを呼び出すことですget_Itemメソッドは、同じものです。

関連する問題