简体   繁体   English

LINQ选择所有项目数组元素

[英]LINQ selecting all item array elements

Strange question, basically I'm creating a DataTable, using the AsEnumerable() method and then using a LINQ query.奇怪的问题,基本上我正在创建一个 DataTable,使用 AsEnumerable() 方法,然后使用 LINQ 查询。

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?但是,我不知道这个 DataTable 有多少行或列,我需要选择所有的项目数组元素,有没有办法从 ItemArray[0] 循环到 ItemArray[ItemArray.lenth-1] LINQ 查询? 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!这是我发现可以绑定到 MVC 网格的唯一方法,所以如果有更好的方法,我很想知道!

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() :使用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:听起来好像您需要SelectMany() ,但作为查询表达式更容易阅读:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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