简体   繁体   English

如何根据其中的列表值使用linq过滤列表?

[英]How can I filter a List with linq based on a value of a list within?

I have a model that has a list of a list: List 1 = States List 2 = Zip Codes 我有一个具有列表列表的模型:列表1 =国家列表2 =邮政编码

Zip Codes has a property that sees if it's been checked (bool) & I am displaying all the states and a list of zip codes they could select the zip codes from a list of checkboxes and when the model gets posted back on HttpPost then checked gets set to true. 邮政编码具有一个属性,可以查看是否已选中(布尔),并且我正在显示所有状态和邮政编码列表,他们可以从复选框列表中选择邮政编码,并在将模型放回HttpPost上然后检查得到时设置为true。

I need to filter the list of states to pull back only the states that have zip codes checked. 我需要过滤状态列表,以仅拉回已检查邮政编码的状态。 I tried this but it comes back null: 我试过了,但返回null:

var temp = States.Where(a => a.ZipCodes.Where(b => b.Checked == true).Any()).ToList();

您可以删除嵌套的Where()调用并进行调用

var temp = States.Where(a => a.ZipCodes.Any(b => b.Checked)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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