使用Linq合并列表列表 [英] Union A List of Lists Using Linq
本文介绍了使用Linq合并列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这不是一个复杂的问题,但是我不能用linq包住头.
This isn't that complicated of a question, but I can't wrap my head around it in linq.
我有一个Enumerable<T>
包含Enumerable<string>
:
public class
{
List<List<string>> ListOfLists = new List<List<string>>();
}
我基本上想从ListOfLists返回每个唯一的字符串;使用foreach循环和存储变量很容易(我可能会提高在最后没有不重复字符的效率,但这不是重点):
I basically want to return each unique string from ListOfLists; this is easy using a foreach loop and a storage variable (I could probably improve the efficiency of not having the distinct at the very end, but that's not the point):
List<string> result = new List<string>();
foreach (var v in ListOfLists)
{
foreach (var s in v)
{
result.Add(s);
}
}
result.Distinct();
如何使用linq做到这一点?
How do I do this with linq?
推荐答案
使用SelectMany
.请参阅 http://msdn.microsoft.com/en-us/library/bb534336.aspx
这篇关于使用Linq合并列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文