0

I have the following Linq query

public static List<string> selectedLocations = new List<string>();

// I then populate selectedLocations with a number of difference strings, each
// corresponding to a valid Location

viewModel.people = (from c in db.People
                    select c)
                    .OrderBy(x => x.Name)
                    .ToList();

// Here I'm basically filtering my dataset to include Locations from
// my array of selectedLocations

viewModel.people = from c in viewModel.people
                    where (
                    from a in selectedLocations
                    where a == c.Location.Name
                    select a
                    ).Any()
                    select c;

This works really well as each Person record can have a single Location.

My question is how do I change this query if a Person can have a one-many relationship with Location? So a Person can have 2 Locations as an example, what do I need to change this line to?

where a == c.Location.Name

Thanks!

Evonet
  • 3,600
  • 4
  • 37
  • 83

1 Answers1

4

you can try to replace this part :

where a == c.Location.Name

with this :

where c.Locations.Any(o => o.Name == a)

that will return true if any Location in Locations property has Name equals a.

har07
  • 88,338
  • 12
  • 84
  • 137
  • Follow up question http://stackoverflow.com/questions/22435361/returning-blanks-in-many-to-many-linq-query – Evonet Mar 16 '14 at 09:51