tags:

views:

10

answers:

1

Let's say I have

class objectA
{
string name;
}

class objectB
{
 List<objectA> la = new List<objectA>();
}

main()
{
List<objectB> lb = new List<objectB>();

/*some operations that results in lb having many objectB objects
and each objectB having many objectA.

lb
    objectB1
        objectA1
            somestring1
        objectA2
            somestring2
        objectA3
            somestring3
        .
        .
        objectA166
            somestring4
    objectB2
        objectA167
            somestring5
        objectA168
            somestring6
        objectA169
            somestring7
        .
        .
        objectA176
            somestring8
    .
    .
    .
    objectB5
        objectA267
            somestring9
        objectA268
            somestring10
        objectA269
            somestring11
        .
        .
        objectA276
            somestring12
*/
}

What is the linq query that would get me all the somestring only?

Ideally, I could do something such as

var w = from f in lb select lb.(each objectB).(each objectA).name

my current workaround is to enumerate through the collections and add each name to a list, but I think there must be a LINQ way to do this.

+1  A: 

You could do this:

var w = from b in lb
        from a in b.la
        select a.name;
Matthew Manela
Thanks! Too easy -- I was thinking I was messing with .ToList() and never thought of doing what you just did!
Matt