c# - Create Columns in a datatable with a single expression -
c# - Create Columns in a datatable with a single expression -
i want define datatable columns, column definition part of datatable definition statement.
eg
//valid syntax datatable dt = new datatable(); dt.columns.add("num",typeof(int)); dt.columns.add("num2",typeof(int)); but rather want along lines of
//invalid syntax datatable dt = new datatable(data.columns[] { {"num",typeof(int)}, {"num2",typeof(int)} }; is possible? if is, right syntax.
you can't initialize datatable collection initializer. can utilize addrange method of datacolumncollection, don't think much improve original approach
datatable dt = new datatable(); dt.columns.addrange(new[] { new datacolumn("num", typeof(int)), new datacolumn("num2", typeof(int)) }); you can create extension method add together columns in fluent way:
public static datatable addcolumn<t>(this datatable dt, string name) { dt.columns.add(name, typeof(t)); homecoming dt; } usage:
var dt = new datatable() .addcolumn<int>("num") .addcolumn<int>("num2"); c# datatable
Comments
Post a Comment