Linq: Query A Nested Collectionhttp://forums.asp.net/t/1447579.aspx/1?Linq+Query+A+Nested+CollectionThu, 16 Jul 2009 16:36:58 -040014475793295858http://forums.asp.net/p/1447579/3295858.aspx/1?Linq+Query+A+Nested+CollectionLinq: Query A Nested Collection <p>Here are my objects:</p> <p></p> <p><br> But the compiler throws an error on the foreach: Cannot convert type 'System.Collections.Generic.IEnumerable&lt;string&gt;' to 'string'.</p> <p>Any insights?</p> <p>Thanks,</p> <p>Jason<br> </p> 2009-07-15T21:48:19-04:003295980http://forums.asp.net/p/1447579/3295980.aspx/1?Re+Linq+Query+A+Nested+CollectionRe: Linq: Query A Nested Collection <p>&nbsp;Basically, the two selects returns an IEnumerable&lt;IEnumerable&lt;string&gt;&gt; is the issue.&nbsp; You want to use the SelectMany extension method.&nbsp; Check this out:&nbsp; <a href="http://msdn.microsoft.com/en-us/library/bb534336.aspx">http://msdn.microsoft.com/en-us/library/bb534336.aspx</a></p> 2009-07-15T23:20:44-04:003297802http://forums.asp.net/p/1447579/3297802.aspx/1?Re+Linq+Query+A+Nested+CollectionRe: Linq: Query A Nested Collection <p>Worked great. Here is the working code:</p> <p><br> </p> <p><pre class="prettyprint">apps.SelectMany(x =&gt; x.Groups.Select(y =&gt; y.Name)).Distinct();</pre><br> Thanks for your help.<br> </p> 2009-07-16T16:36:58-04:00