如何在LINQ Where子句中使用多个参数进行搜索

时间:2022-09-13 00:01:22

I have a Travel Project (ASP.NET MVC4). I want to Search Tours with multiple parameters in a LINQ where clause.

我有一个旅游项目(ASP.NET MVC4)。我想在LINQ where子句中使用多个参数搜索Tours。

This is my View:

这是我的观点:

<form class="green" id="formsearchtravel" method="get" action="@Url.Content("~/" + "search")">
                    <div class="col-lg-3 col-sm-6">
                        <select name="destination" >
                                <option value="any">All </option>
                                <option value="India">Ấn độ</option>
                                <option value="thailand">Thailand</option>
                                <option value="japan">Nhật bản</option>
                                <option value="uk">Anh</option>
                            </select>
                    </div>
                    <div class="col-lg-3 col-sm-6">
                       <select name="duration" >
                                <option value="any">All</option>
                                <option value="5">from 0 - 5</option>
                                <option value="10">from 05 -10</option>
                                <option value="15">from 10 -15</option>
                                <option value="16">over 15 days</option>
                            </select>
                    </div>
                    <div class="col-lg-3 col-sm-6">
                        <select name="price" >
                                <option value="any">All</option>
                                <option value="500">A – 0 – 500 $</option>
                                <option value="1000">B – 500 – 1000 $</option>
                                <option value="2000">C – 1000 – 2000 $</option>
                                <option value="3000">D – 2000 – 3000 $</option>
                                <option value="4000">E – Trên 4000 $</option>
                            </select>
                      </div>
                    <div class="col-lg-3 col-sm-6">
                        <input value="Search" type="submit" />
                    </div>
</form>

This is the Controller:

这是控制器:

public IEnumerable<TourSummaryDto> GetSearchTours(string destination, int? duration, int? price)
{
        var query = _tourItemRepository.GetAll();

        return
            ConvertToTourSummaryDtoQuery(
                query.Where(p => p.Category.Name.ToUpper() == destination.ToUpper() &&  p.TourContent.Giagoc > 500 && p.TourContent.Soluongngay < 15).OrderByDescending(t => t.CreatedDate));
}

How to Search with Destination = any or Destination = something And Duration = all or Duration = something AND Price = all or Price = somethings

如何搜索Destination = any或Destination = something和Duration = all或Duration = something AND Price = all或Price = somethings

How to Search with all of them ( Destination and Duration and Price )

如何搜索所有这些(目的地和持续时间和价格)

Any help is appreciated.

任何帮助表示赞赏。

1 个解决方案

#1


You can build up your where clause based on the input given.

您可以根据给定的输入构建where子句。

For example:

public IEnumerable<TourSummaryDto> GetSearchTours(string destination, int? duration, int? price)
{
        var query = _tourItemRepository.GetAll();

        //always has a destination
        query = query.Where(p => p.Category.Name.ToUpper() == destination.ToUpper();

        //filter results that also have the given duration, only if a value is provided.
        if (duration.HasValue)
        {
            query = query.Where(p => p.Duration == duration.Value);
        }

        return
            ConvertToTourSummaryDtoQuery(query.OrderByDescending(t => t.CreatedDate));
}

This will obviously AND the conditions together. If you have an OR condition (specifically, where limited to two input values) then you will need to combine them into a single where clause.

这显然和条件在一起。如果您有OR条件(特别是限制为两个输入值),那么您需要将它们组合成一个where子句。

#1


You can build up your where clause based on the input given.

您可以根据给定的输入构建where子句。

For example:

public IEnumerable<TourSummaryDto> GetSearchTours(string destination, int? duration, int? price)
{
        var query = _tourItemRepository.GetAll();

        //always has a destination
        query = query.Where(p => p.Category.Name.ToUpper() == destination.ToUpper();

        //filter results that also have the given duration, only if a value is provided.
        if (duration.HasValue)
        {
            query = query.Where(p => p.Duration == duration.Value);
        }

        return
            ConvertToTourSummaryDtoQuery(query.OrderByDescending(t => t.CreatedDate));
}

This will obviously AND the conditions together. If you have an OR condition (specifically, where limited to two input values) then you will need to combine them into a single where clause.

这显然和条件在一起。如果您有OR条件(特别是限制为两个输入值),那么您需要将它们组合成一个where子句。