LINQ - Get all items in a List within a List?

52,872

Solution 1

You want to use the SelectMany extension method.

_tables.SelectMany(t => t.Indexes)

Solution 2

In addition to tbischel's answer, the query expression version of what you're going for is below.

var indexes = from TableInfo tab in _tables 
              from index in tab.Indexes
              select index;

Solution 3

You don't need the where clause and you also shouldn't need to tell it what tab is

And you will need to use SelectMany

var indexes = (from tab in _tables).SelectMany(t => t.Indexes)

Or you could do it like this

   var indexes = from tab in _tables
                  from t in tab.Indexes
                  select t;

That should be a little more familiar syntaz

Solution 4

var rows = from item in table select item;
Share:
52,872
Sonny Boy
Author by

Sonny Boy

Just your average jack of all trades.

Updated on September 03, 2020

Comments

  • Sonny Boy
    Sonny Boy over 3 years

    I'm currently working my way through the learning curve that is LINQ and I could really use some assistance. I don't know if what I want is possible, but if I had to wager, I bet it is.

    I currently have a list of objects called _tables and each of these objects has within it another list of objects exposed through the property, "Indexes". Essentially, I'd like to end up with one List that contains all the Indexes from all of the _tables.

    Here's what I have so far:

    var indexes = from TableInfo tab
                  in _tables
                  where tab.Indexes.Count > 0
                  select tab.Indexes;
    

    Unfortunately, this seems to be giving me another List of Lists, but only where the Indexes List contains more than one value... Is there some way to get all of these lists together without loops?

  • Callum Rogers
    Callum Rogers almost 14 years
  • Pxaml
    Pxaml over 4 years
    what if t.indexes have another list. I would like to get for example the name inside Indexes