Add Conditional Join Dynamically with Linq

2020-05-09 16:58发布

问题:

I have a basic search control which lists companies from a CRM depending on predefined search/filtering criteria supplied by dropdowns. The default selection is "ALL" for each DropDown, otherwise the user chooses a specific item(s). I'd like to be able to construct a Linq query dynamically based on the selections. Out of the 5 selectors they supply values that I can match against the Company table, but two of the selectors (if either or both are chosen) would require a join or joins, else no action should be taken again the base result set. I hope this makes sense.

I'm not sure how to do this effectively. Here is my code:

private void Search()
{
    EnergyPubsCRMDataContext dc = new EnergyPubsCRMDataContext();

    var results = (from c in dc.Companies
                   select c);


    //only create the join if the selected index > 0
    if (ddlIndustry.SelectedIndex > 0)
    {
        //A company can be in 1 or more industries, thus here I want to join
        //with the CompanyIndustry table and have a WHERE clause to match on the ddlIndustry.SelectedValue
    }

    //only create the join if the selected index > 0
    if (ddlServices.SelectedIndex > 0)
    {
        //A company can offer 1 or more services. Here I want to join to the CompanyService table
        //on the CompanyID and have a WHERE clause to match the ddlServices.SelectedValue
    }        

    //These work OK to shape the overal query further (they don't need joins)
    if (ddlCountry.SelectedIndex > 0)
        results = results.Where(c => c.CountryID == Convert.ToInt32(ddlCountry.SelectedValue));

    if (ddlStateRegion.SelectedIndex > 0)
        results = results.Where(c => c.StateRegionID == Convert.ToInt32(ddlStateRegion.SelectedValue));

    if (ddlAccountManagers.SelectedIndex > 0)
    {
        Guid g = new Guid(ddlAccountManagers.SelectedValue);
        results = results.Where(c => c.UserId == g);
    }

    results = results.OrderBy(c => c.CompanyName);

    //Bind to Grid....        
}

回答1:

if (ddlIndustry.SelectedIndex > 0)
{
    //A company can be in 1 or more industries, thus here I want to join
    //with the CompanyIndustry table and have a WHERE clause to match on the ddlIndustry.SelectedValue
    results = results.Where(c => c.CompanyIndustry.IndustryID == ddlIndustry.SelectedValue);
}

Assuming you have correct foreign keys in your database/DBML.

This will generate the join implicitly.



回答2:

I had very similar issue and no foreign keys I could leverage. My solution would translate to something like this:

results = results
  .Join(dc.CompanyIndustry, c => c.CompanyID, ci => ci.CompanyID, (c, ci) => new { c, ci.IndustryID })
  .Where (a => a.IndustryID == ddlIndustry.SelectedValue)
  .Select(a => a.c);

Basically:

1) first we create a join, with a projection that gives us IndustryID (join)

2) we filter based on IndustryID (where)

3) we return original anonymous type, so that we can modify original query (select)