Using LINQ, select list of objects inside another list of objects

只愿长相守 提交于 2019-12-02 17:01:54

You're trying to select multiple result objects for each ClassB object in the original list.

Therefore, you're looking for the SelectMany extension method:

var results = classBList.SelectMany(b => b.MyObjects).Distinct();

If you want to use query expressions, you'll need to use two from clauses:

var results = (from b in classBList from a in b.MyObjects select a).Distinct();

You want to use IEnumerable.SelectMany() Extension Method to flatten the hierarchy:

var result = classBList.SelectMany(b => b.MyObjects).Distinct();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!