This isn't that complicated of a question, but I can't wrap my head around it in linq.
I have an Enumerable<T>
containing an Enumerable<string>
:
public class
{
List<List<string>> ListOfLists = new List<List<string>>();
}
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();
How do I do this with linq?
var distinctStrings = ListOfLists.SelectMany(list => list).Distinct();
Use SelectMany
. See http://msdn.microsoft.com/en-us/library/bb534336.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With