11

一般的にフィルタリングを処理する拡張メソッドを作成して、コードを少し整理しようとしています。

これが私がきれいにしようとしているコードです。

var queryResult = (from r in dc.Retailers select r);
if (!string.IsNullOrEmpty(firstName))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(firstName.Trim(), ex.FirstName.Trim()) > 0);
if (!string.IsNullOrEmpty(lastName))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(lastName.Trim(), ex.LastName.Trim()) > 0);
if (!string.IsNullOrEmpty(companyName))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(companyName.Trim(), ex.CompanyName.Trim()) > 0);
if (!string.IsNullOrEmpty(phone))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(phone.Trim(), ex.Phone.Trim()) > 0);
if (!string.IsNullOrEmpty(email))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(email.Trim(), ex.Email.Trim()) > 0);
if (!string.IsNullOrEmpty(city))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(city.Trim(), ex.City.Trim()) > 0);
if (!string.IsNullOrEmpty(zip))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(zip.Trim(), ex.Zip.Trim()) > 0);
if (!string.IsNullOrEmpty(country))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(country.Trim(), ex.Country.Trim()) > 0);
if (!string.IsNullOrEmpty(state))
    queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(state.Trim(), ex.State.Trim()) > 0);

それは明らかに非常に反復的です。そこで、リフレクションを使用してプロパティでフィルタリングする拡張メソッドを作成しようとしました。これが方法です。

public static void FilterByValue<T>(this IQueryable<T> obj, string propertyName, string propertyValue)
{
    if (!string.IsNullOrEmpty(propertyValue))
    {
        obj =
            obj.Where(
                ex =>
                    SqlFunctions.PatIndex(propertyValue.Trim(), (string)typeof(T).GetProperty(propertyName,
                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).GetValue(ex)) > 0
                    );
    }
}

そして、それは次のように呼び出されます:

var queryResult = (from r in dc.Retailers select r);
queryResult.FilterByValue("firstname", firstName);

しかし、linq を実行するとエラーが発生し、"GetValue" が linq to entity で認識されないことが示されます。

それで、これをきれいにする他の方法はありますか、それとも醜いままにしておく必要がありますか?

4

1 に答える 1

17

技術的には、はい、それを行うことができますが、Expressionに渡すために自分自身を構築する必要がありますWhere

つまり、プロパティを文字列値として受け入れるのではなく、をExpression<Func<T, string>>パラメーターとして受け入れることを検討する必要があります。これにより、選択したオブジェクトが有効であることを確認するためのコンパイル時のサポートが得られます。

一般的な部分を表す式から始めます。*2* パラメーター、オブジェクトと指定されたプロパティの値を持つ関数を表します。次に、その 2 番目のパラメーターのすべてのインスタンスを、実際のメソッドのパラメーターで定義したプロパティ セレクターに置き換えることができます。

public static IQueryable<T> FilterByValue<T>(
    this IQueryable<T> obj,
    Expression<Func<T, string>> propertySelector,
    string propertyValue)
{
    if (!string.IsNullOrEmpty(propertyValue))
    {
        Expression<Func<T, string, bool>> expression =
            (ex, value) => SqlFunctions.PatIndex(propertyValue.Trim(),
                value.Trim()) > 0;

        var newSelector = propertySelector.Body.Replace(
            propertySelector.Parameters[0],
            expression.Parameters[0]);

        var body = expression.Body.Replace(expression.Parameters[1], 
            newSelector);
        var lambda = Expression.Lambda<Func<T, bool>>(
            body, expression.Parameters[0]);

        return obj.Where(lambda);
    }
    else
        return obj;
}

また、このメソッドは関数を使用して、特定の式内の 1 つの式のすべてのインスタンスを別のインスタンスに置き換えます。その実装は次のとおりです。

public class ReplaceVisitor : ExpressionVisitor
{
    private readonly Expression from, to;
    public ReplaceVisitor(Expression from, Expression to)
    {
        this.from = from;
        this.to = to;
    }
    public override Expression Visit(Expression node)
    {
        return node == from ? to : base.Visit(node);
    }
}

public static Expression Replace(this Expression expression,
    Expression searchEx, Expression replaceEx)
{
    return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}

本当にプロパティ名を文字列として受け入れたい場合は、 の定義をnewSelector次のように置き換えます。

var newSelector = Expression.Property(expression.Parameters[0], propertyName);
于 2013-11-01T19:04:50.407 に答える