加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用linq [list]将列表列表转换为单个列表

发布时间:2020-12-16 00:24:39 所属栏目:百科 来源:网络整理
导读:参见英文答案 How to merge a list of lists with same type of items to a single list of items?????????????????????????????????????4个 我是具有相同类型对象的列表列表,我正在尝试将其转换为使用linq的所有对象的单个列表. 我怎样才能做到这一点? 这
参见英文答案 > How to merge a list of lists with same type of items to a single list of items?????????????????????????????????????4个
我是具有相同类型对象的列表列表,我正在尝试将其转换为使用linq的所有对象的单个列表.

我怎样才能做到这一点?

这是我目前的代码:

var allTrackAreas =
            _routables.Select(
                routable =>
                    _centrifugeHelper.GetTrackAreasFromRoutable(routable,_trackAreaCutterParameters,_allowedDestination.MatchedAreas[routable]))
                .Where(area => area != null).ToList();

        foreach (var testAction in _trackAreaCutterParameters.ConcurrentBagTestActions)
        {
            if (allTrackAreas.Any(areas => areas.Any(area => area.Id == testAction.TrackAreaId)))
            {
                currentActions.Add(testAction);
            }
        }

变量allTrackAreas是一个列表列表,我正在使用两次Any,它对效率非常不利.如果它是一个简单的列表会好得多.

解决方法

你可以做:

var allTrackAreasCombined = allTrackAreas.SelectMany(t => t).ToList();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读