0

I already have a city parameter which represent a cityname that I'll search on my DB, everything work well when I do mysite/List?city=mycityname, but what I'm trying to do is that I would also like to do a search by firstname combined with the cityname example List?city=mycityname&firstName=myfirstname. How can I do this ? Here is my query for city, I also added the firstname parameter but I don't really know how to add it so it'll filter both.

public string CurrentFirstName { get; set; }

public ViewResult List(string city, string firstName, int page = 1)
    {
        UsersListViewModel model = new UsersListViewModel
        {
            Users = repository.Userss
            .Where(p =>city == null || p.CityName == city )
            .OrderBy(p => p.UsersId)
            .Skip((page - 1) * PageSize)
            .Take(PageSize),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                UsersPerPage = PageSize,
                TotalUsers = repository.Userss.Count()
            },
            CurrentCity = city
            // CurrentFirstName = firstName
        };
        return View(model);
    }
4

3 回答 3

1

你可以写这样的东西

Users = repository.Userss
        .Where(p =>city == null || p.CityName == city )
        .Where(p=> firstName == null || p.FirstName == firstName)
        .OrderBy(p => p.UsersId)
     // rest of your query
于 2013-05-06T04:23:28.150 回答
1

您可以进行一些条件查询构建,例如,

public ViewResult List(string city, string firstName, int page = 1)
{
    var query = repository.Userss.Where(p => city == null || p.CityName == city);
    if (firstName != null)
        query = query.Where(p => firstName == null || p.FirstName == firstName);

    var model = new UsersListViewModel
    {
        Users = query
        .OrderBy(p => p.UsersId)
        .Skip((page - 1) * PageSize)
        .Take(PageSize),
        PagingInfo = new PagingInfo
        {
            CurrentPage = page,
            UsersPerPage = PageSize,
            TotalUsers = repository.Userss.Count()
        },
        CurrentCity = city
        // CurrentFirstName = firstName
    };
    return View(model);
}

注意:我认为您还应该考虑TotalUsers计算的搜索条件

希望这可以帮助。

于 2013-05-06T07:31:39.647 回答
1

看看下面的代码:

    public ViewResult List(string city, string firstName, int page = 1)
    {
        UsersListViewModel model = new UsersListViewModel
        {
            Users = repository.Userss
            .Where((p =>city == null || p.CityName == city ) && 
             (p =>firstname == null || p.FirstName == firstName))
            .OrderBy(p => p.UsersId)
            .Skip((page - 1) * PageSize)
            .Take(PageSize),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                UsersPerPage = PageSize,
                TotalUsers = repository.Userss.Count()
            },
            CurrentCity = city
            CurrentFirstName = firstName
        };
        return View(model);
    }
于 2013-05-06T04:42:34.027 回答