1

i am using asp.net mvc3 and i am going to implement advance search
this is my advance search form

 @using (Html.BeginForm("AdvanceSearch","Coupon",FormMethod.Post))
    {
        @Html.ValidationSummary(true)
        <fieldset>

            <div class="editor-label">
                @Html.LabelFor(model => model.AdvanceSearch.CouponName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.AdvanceSearch.CouponName)
                @Html.ValidationMessageFor(model => model.AdvanceSearch.CouponName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.AdvanceSearch.Category)
            </div>
            <div class="editor-field">
                @Html.DropDownList("categories", new SelectList(Model.AdvanceSearch.Category.OrderBy(c=>c.Name).Select(c => c.Name)), "--- Select Categories ---")
                @Html.ValidationMessageFor(model => model.AdvanceSearch.Category)
            </div>
            <div class="editor-label">

                <div class="editor-label">
                    @Html.LabelFor(model => model.AdvanceSearch.CreateDate)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.AdvanceSearch.CreateDate, new { @class = "picker" })
                    @Html.ValidationMessageFor(model => model.AdvanceSearch.CreateDate)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.AdvanceSearch.ExpiredDate)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.AdvanceSearch.ExpiredDate, new { @class = "picker" })
                    @Html.ValidationMessageFor(model => model.AdvanceSearch.ExpiredDate)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.AdvanceSearch.PublishDate)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.AdvanceSearch.PublishDate, new { @class = "picker" })
                    @Html.ValidationMessageFor(model => model.AdvanceSearch.PublishDate)
                </div>
                @Html.LabelFor(model => model.AdvanceSearch.Company)
            </div>
            <div class="editor-field">
                @Html.DropDownList("companies", new SelectList(Model.AdvanceSearch.Company.OrderBy(c=>c.Name).Select(c => c.Name)), "--- Select Companies ---")
                @Html.ValidationMessageFor(model => model.AdvanceSearch.Company)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.AdvanceSearch.Description)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.AdvanceSearch.Description)
                @Html.ValidationMessageFor(model => model.AdvanceSearch.Description)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.AdvanceSearch.IsPublish)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.AdvanceSearch.IsPublish)
                @Html.ValidationMessageFor(model => model.AdvanceSearch.IsPublish)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.AdvanceSearch.Active)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.AdvanceSearch.Active)
                @Html.ValidationMessageFor(model => model.AdvanceSearch.Active)
            </div>

            <p>
                <input type="submit" value="Search" />
            </p>
        </fieldset>
    }

now the problem is that i have following scenarios

  • if only CouponName is given then all coupons contains specified name will be returned
  • if any company is selected from company list then we will search all coupon from that company having specified name will be returned
  • if any Category is selected from category list then we will search all coupon from that category having specified name will be returned
  • if any date from all of threee date is been selected then we will filter coupon by that date

now i am confused that what is the best way to do that , should i implement if else conditions , switch or what ?

4

1 に答える 1

0

「最善の方法」があるかどうかわからない。それはすべて、フィルターをどのように適用しているかによって異なります。

LINQを使用してフィルターを適用し、基本式を使用してフィルター構造を記述し、提供されたすべてのフィルター選択に対してAND式を記述します。これを簡単にするために、いくつかの拡張メソッドを使用します。次に、フィルター式を段階的に作成し、複雑になるにつれて、それに応じて手順を実装できます。各基本ステップには、特定の部分が指定されているかどうか、およびそれが基本式に追加されているかどうかを確認するためのifチェックがあります。

// Example of service for applying filter
public GetProductsResponse GetProducts(GetProductsRequest request)
{
    // base expression is true, ie always returns everything
    Expression<Func<ProductEntity, bool>> filterExpression = i => true;

    // subsequent filters narrow base expression results
    if (!request.IncludeDeleted)
        filterExpression = filterExpression.And(i => i
            .IsDeleted == false);

    // I like to use nullable types so that I can check whether a parameter is specified or not
    if (request.CouponName != null)
        filterExpression = filterExpression.And(i => 
            i.CouponName == request.CouponName.Value);

    if (request.Company!= null)
        filterExpression = filterExpression.And(i => 
            i.Company== request.Company.Value);

    // etc.

    var products = _dataContext.Products.Where(filterExpression);

    // you probably want to transform products into some sort of model specific class

    return new GetProductsRequest
    {
        products = products;
    };
}


// Extension methods for Linq And and Or helpers
public static class Utility
{
    public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
    {
        // build parameter map (from parameters of second to parameters of first)
        var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);

        // replace parameters in the second lambda expression with parameters from the first
        var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

        // apply composition of lambda expression bodies to parameters from the first expression 
        return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.And);
    }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.Or);
    }
}
于 2012-11-23T11:13:02.810 に答える