简体   繁体   中英

LINQ selecting all item array elements

Strange question, basically I'm creating a DataTable, using the AsEnumerable() method and then using a LINQ query.

However, I don't know how many rows or columns this DataTable has, and I need to select all of the item array elements, is there any way of cycling through from ItemArray[0] to ItemArray[ItemArray.lenth-1] in a LINQ query? This is the only way I've found that can bind to an MVC grid, so if there's a better way I'd love to know!

Below is a kind of pseudocode of what i'd like to do. Any help would be great!

var model = from t in table.AsEnumerable().AsQueryable()
    select new
    { 
        for (i = 0 to ItemArray.length)
        {
            t.ItemArray[i]
        }
    }

Use SelectMany() :

var items = table.AsEnumerable().SelectMany(row=>row.ItemArray);

You can also group the items by row:

var items = from row in dt.AsEnumerable()
            from item in row.ItemArray
            group new {item} by row;

It does sound as if you need a SelectMany() , but it will be easier to read as a query expression:

var model = from row in table.AsEnumerable()
            from item in row.ItemArray
            select item;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM