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);
    }

3 Answers 3

1

You can do some conditional query building like,

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);
}

Note: i think you should also consider the search criteria on TotalUsers calculation

hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

You can write something like this

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

Comments

1

Have a look into the following code:

    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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.