我有以下Linq查询
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;
这工作得很好,因为每个Person记录可以有一个Location。
我的问题是我如何改变这个查询,如果一个人可以有一个多与位置的关系?一个人可以有2个地点作为例子,我需要把这行改成什么?
where a == c.Location.Name
谢谢!
您可以尝试替换这部分:
where a == c.Location.Name
with this:
where c.Locations.Any(o => o.Name == a)
如果Locations
属性中的任何Location
的Name
等于a
,则返回true