开发者

How can I select objects sub members with linq?

开发者 https://www.devze.com 2022-12-19 03:11 出处:网络
Let\'s say I have class objectA { string name; } class objectB { List<objectA&开发者_开发问答gt; la = new List<objectA>();

Let's say I have

class objectA
{
string name;
}

class objectB
{
 List<objectA&开发者_开发问答gt; 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.


You could do this:

var w = from b in lb
        from a in b.la
        select a.name;
0

精彩评论

暂无评论...
验证码 换一张
取 消