I know we can easily do this by a simple loop, but I want to persue this LINQ/Predicate?
我知道我们可以通过一个简单的循环来实现这一点,但是我想要说服这个LINQ/谓词?
string[] columnNames = dt.Columns.?
or
string[] columnNames = from DataColumn dc in dt.Columns select dc.name;
4 个解决方案
#1
174
Try this (LINQ method syntax):
试试这个(LINQ方法语法):
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
or in LINQ Query syntax:
或在LINQ查询语法中:
string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>()
select dc.ColumnName).ToArray();
Cast
is required, because Columns is of type DataColumnCollection which is a IEnumerable
, not IEnumerable<DataColumn>
. The other parts should be obvious.
强制转换是必需的,因为列是DataColumnCollection类型,它是一个IEnumerable,不是IEnumerable,而是
#2
14
Use
使用
var arrayNames = (from DataColumn x in dt.Columns
select x.ColumnName).ToArray();
#3
4
I'd suggest using such extension method:
我建议使用这种扩展方法:
public static class DataColumnCollectionExtensions
{
public static IEnumerable<DataColumn> AsEnumerable(this DataColumnCollection source)
{
return source.Cast<DataColumn>();
}
}
And therefore:
因此:
string[] columnNames = dataTable.Columns.AsEnumerable().Select(column => column.Name).ToArray();
You may also implement one more extension method for DataTable
class to reduce code:
您还可以为DataTable类再实现一个扩展方法来减少代码:
public static class DataTableExtensions
{
public static IEnumerable<DataColumn> GetColumns(this DataTable source)
{
return source.Columns.AsEnumerable();
}
}
And use it as follows:
使用方法如下:
string[] columnNames = dataTable.GetColumns().Select(column => column.Name).ToArray();
#4
0
List<String> lsColumns = new List<string>();
if(dt.Rows.Count>0)
{
var count = dt.Rows[0].Table.Columns.Count;
for (int i = 0; i < count;i++ )
{
lsColumns.Add(Convert.ToString(dt.Rows[0][i]));
}
}
#1
174
Try this (LINQ method syntax):
试试这个(LINQ方法语法):
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
or in LINQ Query syntax:
或在LINQ查询语法中:
string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>()
select dc.ColumnName).ToArray();
Cast
is required, because Columns is of type DataColumnCollection which is a IEnumerable
, not IEnumerable<DataColumn>
. The other parts should be obvious.
强制转换是必需的,因为列是DataColumnCollection类型,它是一个IEnumerable,不是IEnumerable,而是
#2
14
Use
使用
var arrayNames = (from DataColumn x in dt.Columns
select x.ColumnName).ToArray();
#3
4
I'd suggest using such extension method:
我建议使用这种扩展方法:
public static class DataColumnCollectionExtensions
{
public static IEnumerable<DataColumn> AsEnumerable(this DataColumnCollection source)
{
return source.Cast<DataColumn>();
}
}
And therefore:
因此:
string[] columnNames = dataTable.Columns.AsEnumerable().Select(column => column.Name).ToArray();
You may also implement one more extension method for DataTable
class to reduce code:
您还可以为DataTable类再实现一个扩展方法来减少代码:
public static class DataTableExtensions
{
public static IEnumerable<DataColumn> GetColumns(this DataTable source)
{
return source.Columns.AsEnumerable();
}
}
And use it as follows:
使用方法如下:
string[] columnNames = dataTable.GetColumns().Select(column => column.Name).ToArray();
#4
0
List<String> lsColumns = new List<string>();
if(dt.Rows.Count>0)
{
var count = dt.Rows[0].Table.Columns.Count;
for (int i = 0; i < count;i++ )
{
lsColumns.Add(Convert.ToString(dt.Rows[0][i]));
}
}