0

アプリケーションに、より一般的なクエリ機能を構築しようとしています。私がやりたいのは、与えられた述語式が、後で渡される値を持つ queryable に適用できるオブジェクトを定義することです。

以下のコードは、問題を理解するのに十分なほどうまくやろうとしていることを示しているはずです。詳細を知りたい場合はお知らせください。

ありがとう!

//in practice the value of this would be set in object constructor likely
private Expression<Func<Contact, string, bool>> FilterDefinition = (c, val) => c.CompanyName.Contains(val);

//this needs to filter the contacts using the FilterDefinition and the filterValue. Filterval needs to become the string parameter
private IQueryable<Contact> ApplyFilter(IQueryable<Contact> contacts, string filterValue)
{
     //this method is what I do know know how to contruct.
     // I need to take the FilterDefinition expression and create a new expression that would be the result if 'filtervalue' had been passed into it when it was created.
     //ie the result would be (if 'mycompany' was the value of filterValue) an expression of
     //  c => c.CompanyName.Contains("mycompany")
     Expression<Func<Contact, bool>> usableFilter = InjectParametersIntoCriteria(FilterDefinition, "SomeCompanyName");

     //which I could use the results of to filter my full results.
     return contacts.Where(usableFilter);
}
4

2 に答える 2

0

次のコードを ApplyFilter 本文に配置します。

 var f = FilterDefinition.Compile();
 return contacts.Where(x => f(x, filterValue));
于 2010-07-08T16:20:13.163 に答える
0

このようなものをお探しですか?

private Func<string, Expression<Func<Contact, bool>>> FilterDefinition =
    val => c => c.CompanyName.Contains(val);

private IQueryable<Contact> ApplyFilter(
    IQueryable<Contact> contacts, string filterValue)
{
    Expression<Func<Contact, bool>> usableFilter = FilterDefinition(filterValue);

    return contacts.Where(usableFilter);
}

参照:カリー化

于 2010-07-08T16:16:20.663 に答える