简体   繁体   中英

Multiple WHERE IN clause LINQ equivalent

I have a web page where the user can restrict results based on three different multi select choices (please see attached picture).

A bit of background. Documents have many sectors, companies and analysts assigned to them. My EF objects have navigation properties which means I don't need to write explicit JOINS to get the data I need.

My problem is I can't construct the LINQ query I need to get the results I need. Using SQL it would be simple and I could easily use a combination of JOINS and WHERE IN ('Blah', 'Another blah'). In my controller I have the following code:

public JsonResult FilterResearch(FilterResearchModel filterResearch)
{
        var db = new Context();

        // home model
        var model = new HomeModel();

        var selectedSectors = from s in db.Sectors
                              where filterResearch.SelectedSectors.Contains(s.Name)
                              select s;

        var selectedCompanies = from c in db.Companies
                              where filterResearch.SelectedCompanies.Contains(c.Name)
                              select c;

        var selectedAnalysts = from a in db.Analysts
                              where filterResearch.SelectedAnalysts.Contains(a.Name)
                              select a;

        var filteredResults = from d in db.Documents
                      where selectedSectors.Contains(d.Sectors)
                      select d;

FilterResearch.Selected"Something" are string arrays. My "filteredResults" query is what should contain the filtered documents I plan to return.

EDIT Some people have commented on the fact I'm not being clear. I'm trying to filter my documents based on 3 string arrays. Each of these string arrays are navigation properties in the "document" object. On the client the user has three multi select controls so each array can have more than one element. Now they can choose any of the three and choose as many options as they wish.

THIS IS THE PROBLEM When I compile it I get the following error: "cannot convert from 'System.Linq.IQueryable' to 'System.Linq.ParallelQuery>"

EDIT AGAIN Picture of error included. It occurs on "where selectedSectors.Contains(d.Sectors)" 在此处输入图片说明

I have checked:

with little luck. Is there a way where I can just say "filter the documents based on the companies AND sectors AND analysts?

Maybe I'm misunderstanding this (and I don't know your full object model) but wouldn't the following work...

    var filteredResults = from d in db.Documents
                         where d.Sectors.Exists(sect => filterResearch.SelectedSectors.Contains(sect.Name))
                            && d.Companies.Exists(comp => filterResearch.SelectedCompanies.Contains(comp.Name))
                            && d.Analysts.Exists(anyst => filterResearch.SelectedAnalysts.Contains(anyst.Name))
                        select d;

Edit: Just to add, I haven't tried this in Visual Studio so it may not compile. Maybe you'd need to use a function other than Exists - Any perhaps - but I'm sure you get the gist of what I'm thinking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM