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

c# – 如何在不使用foreach的情况下获得字典中嵌套列表的数量总

发布时间:2020-12-15 04:09:24 所属栏目:百科 来源:网络整理
导读:我想在以下词典中获取列表中的项目总数: Dictionaryint,Liststring dd = new Dictionaryint,Liststring() { {1,new Liststring {"cem"}},{2,new Liststring {"cem","canan"}},{3,new Liststring {"canan","cenk","cem"}}};// This only returns an enumerat
我想在以下词典中获取列表中的项目总数:
Dictionary<int,List<string>> dd = new Dictionary<int,List<string>>() {
    {1,new List<string> {"cem"}},{2,new List<string> {"cem","canan"}},{3,new List<string> {"canan","cenk","cem"}}
};

// This only returns an enumerated array.
var i = (from c in dd
         select c.Value.Count).Select(p=>p);

解决方法

我相信这会让你有效而清晰地获得你想要的数量.在引擎盖下它必须遍历列表,但要获得总计数,没有办法避免这种情况.
var i = dd.Values.Sum(x => x.Count);

(编辑:李大同)

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

    推荐文章
      热点阅读