特定のタイプの検索用に独自のリフレクション関数を構築しています。
問題は、ID のリスト内で ID のグループを検索し、検索/選択クエリをフィルタリングして、これらの特定のオブジェクトのみを取得することです。
これは、Linq-Entity フレームワークで "IN()" を使用するのと同じです。しかし、a.objid は使用できません。
return query.Where(a => ObjectsToFind.Contains(a.objid));
ただし、T テンプレートを使用しているため、「a.objid」がエラーを引き起こしています。
したがって、a は「MyTable a」ではなく「T a」であるため、「objid」プロパティと呼ぶことができます。
パラメータ式でこれを行う方法があることは知っています。しかし、私はそれを理解することはできません。
上記の行を次の行に置き換えようとしたのは次のとおりです。
public static IQueryable<T> WhereFunctionContains<T>(this IQueryable<T> query, string contains)
{
var ObjectsToFind = new List<int>(); // I want to search IN() this function that gets filled in here.
ObjectsToFind = FillObjectsToFind(); // just the object id integers I want to filter
var parameter = Expression.Parameter(typeof(T), "type");
var propertyExpression = Expression.Property(parameter, "objid"); // I look for this
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(int) }); // int type
var vars = Expression.Variable(List<int>,); // I can't use "constant", but what do I use here?
var containsExpression = Expression.Call(propertyExpression, method, vars);
return query.Where(Expression.Lambda<Func<T, bool>>(containsExpression, parameter));
}
「T」を実際のテーブル エンティティに置き換えると、多くの問題が発生するため、T をそのまま使用することにしました。
else if (s.ST == "function")
{ // search func
myTable a;
DC2 = MyUtility.WhereFunctionContains(DC2, a => a.objid, s.S);
// s.S is my search query string.
// s.ST means I'm searching for functions (from another associated table)
// I don't understand how a.objid gets called, I wanted to use Template/Reflections.
}
Zev の関数を呼び出す方法を次に示します。
public static IQueryable<T> WhereFunctionContains<T>(this IQueryable<T> query, Expression<Func<T, int>> converter, string contains)
{
FunctionsClass fc = new FunctionsClass();
var ObjectsToFind = new List<int>();
ObjectsToFind = fc.SearchContainFunction(contains); // I grabbed my list of IDs to search
return query.Where(t => ObjectsToFind.Contains(converter(t)));
}