我有一个带有# of Students
等列的DataTable
,并希望按降序排序。这是我的代码:
...
dt.Columns.Add(new DataColumn("# of Students", typeof(string)));
// do some stuff... add records etc.
// A runtime error occurs here: "Cannot find column '# of Students'"
var rows = dt.Select("","'# of Students' desc");
// this is just fine.
rows = dt.Select("","# of Students");
如果名称中包含特殊字符,如何访问此列?
你可以同时使用 [] 或 '' 语法。以下两个代码段都是正确的:
var rows = dt.Select("","`# of Students` desc");
var rows = dt.Select("","[# of Students] desc");
你应该使用 [] 括号,像这样:
var rows = dt.Select("","[# of Students] desc");