且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

作为LINQ嵌套的foreach,带有链接的链表

更新时间:2022-11-27 22:39:38

首先,使用SelectMany展平列表,然后使用FirstOrDefault进行过滤:

First, use SelectMany to flatten the list, then FirstOrDefault to filter:

CountryList.SelectMany(c => c.stateList).FirstOrDefault(d => d.Name == passedInValue);

请注意,由于FirstOrDefault可以使用谓词,因此实际上不需要Where子句.

Note that because FirstOrDefault can take a predicate, you don't actually need the Where clause.