Querying DataColumnCollection with LINQ

前端 未结 3 1525
暗喜
暗喜 2021-01-30 16:05

I\'m trying to perform a simple LINQ query on the Columns property of a DataTable:

from c in myDataTable.Columns.AsQueryable()
    select c.ColumnName

相关标签:
3条回答
  • 2021-01-30 16:07

    You could also use:

    var x = from DataColumn c in myDataTable.Columns
            select c.ColumnName
    

    It will effectively do the same as Dave's code: "in a query expression, an explicitly typed iteration variable translates to an invocation of Cast(IEnumerable)", according to the Enumerable.Cast<TResult> Method MSDN article.

    0 讨论(0)
  • 2021-01-30 16:07

    With Linq Method Syntax:

    var x = myDataTable.Columns.Cast<DataColumn>().Select(c => c.ColumnName);
    
    0 讨论(0)
  • 2021-01-30 16:15

    How about:

    var x = from c in dt.Columns.Cast<DataColumn>()
            select c.ColumnName;
    
    0 讨论(0)
提交回复
热议问题