Use of Square Brackets

Use of square brackets [] with a Field Name in a Datatable "Compute" method expression in .Net. 

  1. Sum the values of a specific field of a datatable .

    Datatable.Compute("Compute Function", "") .

    Example: Datatable l_dtEmploye contain "Age" Field ,

    Now if we want to sum the age's of the employee:
    1. dtEmploye.Compute("Sum(Age)""");  
  2. Sum the value of a specific field of a datatable on the basis of a specific condition or filter.

    Datatable.Compute("Compute Function", "particular condition") .

    Example: Suppose a datatable contains "Age" and "Sex".

    Now if we want to sum the age of male employees:
    1. dtEmploye.Compute("Sum(Age)""Sex = 'M'");  

Until now, we didn't use square brackets with Field name and don't have any issue with the examples above. But if we change the field name "Emp Salary" instead of "Age", now it will give an error because the field name "Emp Salary" contains the special symbol "space".

So for avoiding this error we use [] square brackets with field name .

  1. dtEmploye.Compute("Sum(Emp Salary)""");                 //Error .  
  2. dtEmploye.Compute("Sum([Emp Salary])""");              //correct .  
  3. dtEmploye.Compute("Sum([Emp Salary])""Sex = 'M'");    //correct .  
Suppose we have the following data table:
  1. /// <summary>  
  2. /// This function is use to get data into p_dtData .  
  3. /// </summary>  
  4. /// <returns></returns>  
  5. public System.Data.DataTable GetData()  
  6. {  
  7.       System.Data.DataTable l_dtEmployee = new System.Data.DataTable("Employee");  
  8.   
  9.       // Create columns for p_dtData .   
  10.       l_dtEmployee.Columns.Add("EmpId"typeof(int));  
  11.       l_dtEmployee.Columns.Add("Name"typeof(string));  
  12.       l_dtEmployee.Columns.Add("Sex"typeof(string));  
  13.       l_dtEmployee.Columns.Add("DateOfReport"typeof(DateTime));  
  14.       l_dtEmployee.Columns.Add("City"typeof(string));  
  15.       l_dtEmployee.Columns.Add("Emp Salary"typeof(Decimal));   
  16.       l_dtEmployee.Columns.Add("Leaves"typeof(float));   
  17.   
  18.       // Create rows for p_dtData .  
  19.       l_dtEmployee.Rows.Add(10, "Abhishek""M", DateTime.Now, "Nainital", 5500, .34);  
  20.       l_dtEmployee.Rows.Add(20, "Digvijay""M", DateTime.Now, "Shimla", 4800, .98);  
  21.       l_dtEmployee.Rows.Add(30, "Shrish""M", DateTime.Now, "Dehradun", 6700, .31);  
  22.       l_dtEmployee.Rows.Add(40, "Shaifali""F", DateTime.Now, "Dehradun", 7000, .10);  
  23.       l_dtEmployee.Rows.Add(50, "Sonam""F", DateTime.Now, "Delhi", 6500, .43);  
  24.   
  25.       l_dtEmployee.Rows.Add(60, "Ankur""M", DateTime.Now, "Delhi", 4500, .33);  
  26.       l_dtEmployee.Rows.Add(70, "Vipin""M", DateTime.Now, "Dehradun", 8000, .44);  
  27.       l_dtEmployee.Rows.Add(80, "Jasmeen""F", DateTime.Now, "Delhi", 6000, .65);  
  28.       l_dtEmployee.Rows.Add(90, "Rakesh""M", DateTime.Now, "Jaisalmer", 2000, .32);  
  29.       l_dtEmployee.Rows.Add(100, "Annirud""M", DateTime.Now, "Rohtak", 3900, .22);  
  30.       return l_dtEmployee;  
  31. }  
Now, If we want to compute a sum or count on the basis of specific conditions then "Compute" is a function inside the datatable in .Net.

Datatable.Compute("Compute Function", " On the bases of condition").
  1. System.Data.DataTable l_dtEmp = GetData();  
  2.   
  3. Object l_value = l_dtEmp.Compute("Count([Emp Salary])""Sex = 'M'");  
We should use a field name with square brackets, it will resolve the conflicts if the field name contains any special characker like space "First Name" or "Emp Id". 

Up Next
    Ebook Download
    View all
    Learn
    View all