修改C#集团通过代码,让空间在字段名称
问题描述:
这是参照这个答案提供的代码:由@蒂姆 - Schmelter enter link description here。修改C#集团通过代码,让空间在字段名称
的代码是按特定的列数据表,并添加计数到列(支点):
public DataTable GroupBy(string i_sGroupByColumn, string i_sAggregateColumn, DataTable i_dSourceTable)
{
DataView dv = new DataView(i_dSourceTable);
//getting distinct values for group column
DataTable dtGroup = dv.ToTable(true, new string[] { i_sGroupByColumn });
//adding column for the row count
dtGroup.Columns.Add("Count", typeof(int));
//looping thru distinct values for the group, counting
foreach (DataRow dr in dtGroup.Rows)
{
dr["Count"] = i_dSourceTable.Compute("Count(" + i_sAggregateColumn + ")", i_sGroupByColumn + " = '" + dr[i_sGroupByColumn] + "'");
}
//returning grouped/counted result
return dtGroup;
}
但是,我的表具有列(我不能改变这一点)用在字段名称空间,即:“方案ID”
当我传递到上面的代码,使用此:
DataTable Solutions = GroupBy("Solution ID", "Solution ID", tbQ);
...我得到的错误:语法错误:缺少运算'ID'运营商之后的时代。
我曾尝试用[]封装,修改的空间+,但它不工作。
是否有可能修改上面的代码,以接受有空格的列?
谢谢你,马克
答
I have tried encapsulating with [], amending the space to +, but it doesn't work.
好改变的空间+
肯定是行不通的,但在括号封装宜。请尝试
if(!i_sAggregateColumn.StartsWith("["))
i_sAggregateColumn = "[" + i_sAggregateColumn + "]";
if(!i_sGroupByColumn.StartsWith("["))
i_sGroupByColumn = "[" + i_sGroupByColumn + "]";
请注意,如果您的列名已包含括号you'll have to try something else。
+0
谢谢你 - 我也想出了只在内存中的数据表重命名列:Solutions.Columns [“方案ID”]的ColumnName =“SolutionID”。希望这也有帮助。 – Mark
只是为了仔细检查,但使用'DataTable的解决方案=的GroupBy( “[解决方案ID]”, “[解决方案ID]”,太贝可);'没有工作? – dash