0

I want to know how to write expression in data table to concatenate two integer type columns. Because when i try with '+' it adds the two integers.

Thanks in advance.

Prateek Deshpande
  • 215
  • 1
  • 5
  • 18

2 Answers2

3

If you want to set the expression of the result column. try this.

DataTable table = new DataTable();

table.Columns.Add("OrderCount",typeof(int));

table.Columns.Add("OrderPrice",typeof(int));

table.Rows.Add(new object[] { 1, 1 });

table.Rows.Add(new object[] { 2, 3 });

table.Rows.Add(new object[] { 4, 5 });

table.Columns.Add("Result", typeof(string));

table.Columns["Result"].Expression = "Convert(OrderCount, 'System.String') + OrderPrice";
Akanksha Gaur
  • 2,636
  • 3
  • 26
  • 50
1

The example:

int a = 1;
int b = 2;

var asInt = a + b; // asInt is now 3
var asString = a.ToString() + b.ToString() // asString is now "12"
var asStringAlt = String.Format("{0}{1}", a, b); // alternate method as suggested
yamen
  • 15,390
  • 3
  • 42
  • 52
  • Would be better to do a string.format, performance wise. So `var asString = string.Format("{0}{1}", a, b);`. No need for unnecessary string casting this way. – Jan_V Apr 12 '12 at 07:37
  • @Jan_V: Updated to show both methods. Not sure which is more performant though. – yamen Apr 12 '12 at 07:40
  • In a lot of books they tell string.Format() is the better one (in theory) to use, because of performance and memory issues. But as suggested in this post: http://stackoverflow.com/questions/296978/when-is-it-better-to-use-string-format-vs-string-concatenation It probably doesn't matter much. – Jan_V Apr 12 '12 at 07:58