I am trying to be able to find one of the Column name of a datatable by using the Column index with Linq. Example:
int colindex = 2;
Datatable: PersonInfo
ID Name Address City State Country Zip
1 xx 11-23 LA CA USA 9123
PersonInfo.AsEnumerable().Select(x => x.field<int>(Colindex).ColumnName);
returns Address
Why are you trying to use LINQ to do that? It's as easy as:
var columnName = PersonInfo.Columns[colindex].ColumnName;
You code does not work, because Field<T>
extension method returns T
, so Field<int>
returns int
and there is no way to get column name from that int
value.
If you really have to use LINQ, you should query DataTable.Columns
property, not the rows:
var columnName = PersonInfo.Columns.Cast<DataColumn>().ElementAt(colindex).ColumnName;
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.