Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda or Linq method to search multiple parameters

Tags:

c#

lambda

linq

I have a List of Of Objects and id like to query the list with multiple parameters to whittle down the results on a search page.

     int SecLink = (!string.IsNullOrEmpty(Request.QueryString["Sector"])) ? Convert.ToInt32(Request.QueryString["Sector"]) : 0;
        int LocLink = (!string.IsNullOrEmpty(Request.QueryString["Location"])) ? Convert.ToInt32(Request.QueryString["Location"]) : 0;
        int IndLink = (!string.IsNullOrEmpty(Request.QueryString["Industry"])) ? Convert.ToInt32(Request.QueryString["Industry"]) : 0;
        int VacLink = (!string.IsNullOrEmpty(Request.QueryString["Vacancy"])) ? Convert.ToInt32(Request.QueryString["Vacancy"]) : 0;

        string keyword = Request.QueryString["SearchTerm"];

        var dx = new DataX();
        var lstJobs = dx.GetAllJobs().Where(x => x.SectorLink.Equals(SecLink) && x.LocationLink.Equals(LocLink) && x.IndustryLink.Equals(IndLink) && x.VacancyTypeLink.Equals(VacLink) && x.JobName.Contains(keyword)).ToList();

        if (lstJobs.Count > 0)
        {
            uiRptSearchJobs.DataSource = lstJobs;
            uiRptSearchJobs.DataBind();

            uiLitSearchResults.Text = string.Format("<h4>Search result found {0} matches</h4>", lstJobs.Count);
        }

The search params may be '0' as not selected from the previous page, so the results should reflect his.

This is the querystring im passing:

Default.aspx?section=search&Sector=4&Location=0&Industry=0&Vacancy=0&SearchTerm=

but as you can see they querystring will change with what the user selects from the previous page.

like image 975
Neil Hodges Avatar asked Jul 03 '26 12:07

Neil Hodges


2 Answers

If I understand correctly, you don't want to filter if the value of the parameters is 0? If so, two solutions:

  1. Check if the parameter is equal to 0 in your condition:

    var lstJobs = dx.GetAllJobs().Where(x => 
        (SecLink == 0 || x.SectorLink.Equals(SecLink))
        && (LocLink == 0 || x.LocationLink.Equals(LocLink))
        && (IndLink == 0 || x.IndustryLink.Equals(IndLink))
        && (VacLink == 0 || x.VacancyTypeLink.Equals(VacLink))
        && x.JobName.Contains(keyword)).ToList();
    
  2. Linq goodness, dynamically construct your query:

        var query = dx.GetAllJobs().Where(x => x.JobName.Contains(keyword));
    
        if (SecLink != 0)
        {
            query = query.Where(x => x.SectorLink.Equals(SecLink));
        }
    
        if (LocLink != 0)
        {
            query = query.Where(x => x.LocationLink.Equals(LocLink));
        }
    
        if (IndLink != 0)
        {
            query = query.Where(x => x.IndustryLink.Equals(IndLink));
        }
    
        if (VacLink != 0)
        {
            query = query.Where(x => x.VacancyTypeLink.Equals(VacLink));
        }
    
        var lstJobs = query.ToList();
    
like image 114
Kevin Gosse Avatar answered Jul 06 '26 02:07

Kevin Gosse


One option is to conditionally perform the Where clauses:

If the search terms should be ANDed together:

var lstJobs = dx.GetAllJobs();

if (SecLink > 0)
    lstJobs = lstJobs.Where(x => x.SectorLink.Equals(SecLink))

if (LocLink > 0)
    lstJobs = lstJobs.Where(x => x.LocationLink.Equals(LocLink))

if (IndLink > 0)
    lstJobs = lstJobs.Where(x => x.IndustryLink.Equals(IndLink))

if (VacLink > 0)
    lstJobs = lstJobs.Where(x => x.VacationLink.Equals(VacLink))

// Performance does not suffer because the query will
// not get evaluated until it's required. For example, 
// here we call .ToList, which forces the query to be evaluated.
var result = lstJobs.ToList(); 

However, you've made it clear you need the search terms ORed together. In that case:

var lstJobs = dx.GetAllJobs().Where(x => x.JobName.Contains(keyword));

if (SecLink > 0)
    lstJobs = lstJobs.Union(
        dx.GetAllJobs().Where(x => x.SectorLink.Equals(SecLink))

if (LocLink > 0)
    lstJobs = lstJobs.Union(
        dx.GetAllJobs().Where(x => x.LocationLink.Equals(LocLink))

etc...
like image 21
RB. Avatar answered Jul 06 '26 02:07

RB.