更改linq查询以对许多过滤 [英] Changing a linq query to filter on many-many

查看:59
本文介绍了更改linq查询以对许多过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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;

这非常有效,因为每个个人"记录都可以有一个位置.

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

我的问题是,如果一个人可以与位置"建立一对多关系,该如何更改此查询?因此,一个人可以有2个位置作为示例,我需要将此行更改为什么?

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

谢谢!

推荐答案

您可以尝试替换此部分:

you can try to replace this part :

where a == c.Location.Name

与此:

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

,如果Locations属性中的任何Location具有Name等于a,则将返回true.

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

这篇关于更改linq查询以对许多过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆