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)
'.Call'は' string'の 'MethodInfo'を使用しています。 –
x.Keys [0] .Contains( "a") ' – Rafal
のように生成することを目指すプレーンなC#コードを書いてください。私はそれを理解していない、それはどのようにmongodbクエリのために使用できるかわからない –