DataTable dt = new DataTable();
dt.Columns.Add("ItemName", typeof(String));
dt.Columns.Add("SalebyEmployee", typeof(String));
dt.Columns.Add("ItemRate", typeof(Double));
dt.Columns.Add("Qty", typeof(Int32));
dt.Columns.Add("Amount", typeof(Double));
DataRow oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 1 ";
oRow["SalebyEmployee"] = "Kalpesh";
oRow["ItemRate"] = 10;
oRow["Qty"] = 5;
oRow["Amount"] = 50;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 2 ";
oRow["SalebyEmployee"] = "Kalpesh";
oRow["ItemRate"] = 15;
oRow["Qty"] = 3;
oRow["Amount"] = 45;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 3 ";
oRow["SalebyEmployee"] = "Kalpesh";
oRow["ItemRate"] = 4;
oRow["Qty"] = 20;
oRow["Amount"] = 80;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 1 ";
oRow["SalebyEmployee"] = "Chirag";
oRow["ItemRate"] = 10;
oRow["Qty"] = 20;
oRow["Amount"] = 200;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 2 ";
oRow["SalebyEmployee"] = "Chirag";
oRow["ItemRate"] = 15;
oRow["Qty"] = 30;
oRow["Amount"] = 450;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 3 ";
oRow["SalebyEmployee"] = "Kalpesh";
oRow["ItemRate"] = 10;
oRow["Qty"] = 15;
oRow["Amount"] = 150;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 1 ";
oRow["SalebyEmployee"] = "Rohit";
oRow["ItemRate"] = 11;
oRow["Qty"] = 50;
oRow["Amount"] = 550;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 2 ";
oRow["SalebyEmployee"] = "Rohit";
oRow["ItemRate"] = 10;
oRow["Qty"] = 20;
oRow["Amount"] = 200;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 3 ";
oRow["SalebyEmployee"] = "Rohit";
oRow["ItemRate"] = 10;
oRow["Qty"] = 4;
oRow["Amount"] = 40;
oRow = dt.Rows.Add();
oRow["ItemName"] = "Item No. 4 ";
oRow["SalebyEmployee"] = "Rohit";
oRow["ItemRate"] = 100;
oRow["Qty"] = 50;
oRow["Amount"] = 5000;
var queryResult = from item in dt.AsEnumerable()
group item by new { ItemSale = item.Field<String>("ItemName") + "_" + item.Field<String>("SaleByEmployee") } into grp
orderby grp.Key.ItemSale
select new
{
ItemSaleByEmp = grp.Key.ItemSale,
TotalAmount = grp.Sum(i => i.Field<Double>("Amount"))
};
DataTable dtNew = queryResult.ToList().ToDataTable();
public static DataTable ToDataTable(this IList data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}