我有多个字符串列表,IList<string>
,我想将其合并到一个列表中,显示不同的字符串并为每个项目计数(如字典)。最有效的方法是什么?
最佳答案
LINQ(在代码类型和维护方面肯定是最高效的;整体性能将大约与任何其他方法相同):
如果列表在单独的变量中:
var qry = from s in listA.Concat(listB).Concat(listC) // etc
group s by s into tmp
select new { Item = tmp.Key, Count = tmp.Count() };
如果列表都在(列表的)父列表中:
var qry = from list in lists
from s in list
group s by s into tmp
select new { Item = tmp.Key, Count = tmp.Count() };
那么如果你真的想要一个列表:
var resultList = qry.ToList();
关于c# - 一起添加列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/467467/