Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:41:05+00:00 2026-05-13T23:41:05+00:00

What’s the best way to set up a controller to sort by many (possibly

  • 0

What’s the best way to set up a controller to sort by many (possibly null) criteria? Say for example, I was building a site that sold cars. My CarController has a function Index() which returns an IList of cars to the view, and details on each car are rendered with a partial view.

What’s the best way to structure this? Especially if there are a lot of criteria: Car Type (aka SUV), Car Brand, Car Model, Car Year, Car Price, Car Color, bool IsNew, or if I want to sort by closest to me etc… I’m using NHibernate as my ORM. Should I have just a ton of possible NHibernate queries and figure out which one to choose based on if/then’s in the controller? Or is there a simpler way.

Thanks so much for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:41:06+00:00Added an answer on May 13, 2026 at 11:41 pm

    I have tasks in my system and created special class for searching:

    public class TaskSearchCriteria
    {
        public List<int> Statuses { get; set; }
        public List<int> Severities { get; set; }
        public List<int> CreatedBy { get; set; }
        public List<int> ClosedBy { get; set; }
        public List<int> LastModificationBy { get; set; }
    
        public DateTime? CreationDateFrom { get; set; }
        public DateTime? CreationDateTo { get; set; }
    
        public bool SearchInComments { get; set; }
        public bool SearchInContent { get; set; }
        public bool SearchInTitle { get; set; }
    }
    

    One method in that class applies filters:

    public IQueryable<Task> Filter(IQueryable<Task> tasks)
    {
        return
            FilterDates(
            FilterAssignedTo(
            FilterGroups(
            FilterOperationSystems(
            FilterPlatforms(
            FilterPriorities(
            FilterSeverities(
            FilterVersionsResolved(
            FilterVersionsReported(
            FilterTaskContent(
            FilterStatuses(tasks)))))))))));
    }
    

    This is one of methods used for filtering:

    private IQueryable<Task> FilterSeverities(IQueryable<Task> tasks)
    {
        if (Severities.Contains(TaskSearchConsts.All) || (!Severities.Any()))
            return tasks;
    
        var expressions = new List<Expression>();
    
        if (Severities.Contains(TaskSearchConsts.Active))
            expressions.Add(_taskParameter.EqualExpression("Severity.IsActive", 1));
    
        return tasks.WhereIn(_taskParameter, "Severity.ID", Severities, expressions);
    }
    

    This is Entity Framework, but it can be easily done in nHibernate too by adding where clauses to IQuery.

    To search I have a method in controller:

    [HttpPost]
    public ActionResult TaskSearch([Bind(Prefix = "Search")]TaskSearchCriteria criteria)
    

    Having one class for filtering is nice, because it can be serialized and save to database for future use. This way user can reuse filter parameters.

    You would have:

    public class CarSearchCriteria
    {
        List<int> ListOfCarTypesIds;
        List<int> ListOfCarBrandIds;
        bool IsNew;
        //and more
    }
    

    If list is empty, you don’t apply filter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.