c# - 如果您在 groupBy 和 Sum 之后返回 dataTable,则使用 Linq 或任何其他方式使用 Sum 转换 Datatable GroupBy 多列
问题描述
错误截图
我想汇总 Group BY 之后的所有总和、流量、框列,但它向我显示错误。我想从这个语法库中使用,并且希望 DataTable 不是列表。那里我的示例代码任何人都可以查看和修改此代码,因为我面临困难。
DataTable groupFile = dataTable.AsEnumerable()
.GroupBy(x => new
{
CompanyName = x.Field<string>("CompanyName"),
POS = x.Field<Int32>("POS#"),
storeName = x.Field<string>("storeName"),
SpecialIdentifier = x.Field<Int32>("SpecialIdentifier"),
EmployeeName = x.Field<string>("EmployeeName"),
processDate = x.Field<DateTime>("processDate"),
dow = x.Field<string>("dow"),
monthsName = x.Field<string>("monthsName"),
years = x.Field<Int32>("years"),
MonthDay = x.Field<Int32>("MonthDay"),
processHour = x.Field<Int32>("processHour"),
monitored = x.Field<string>("monitored")
}).Select(x => new groupdatatable
{
CompanyName = x.Key.CompanyName,
POS = x.Key.POS,
storeName = x.Key.storeName,
SpecialIdentifier = x.Key.SpecialIdentifier,
EmployeeName = x.Key.EmployeeName,
processDate = x.Key.processDate,
dow = x.Key.dow,
monthsName = x.Key.monthsName,
years = x.Key.years,
MonthDay = x.Key.MonthDay,
processHour = x.Key.processHour,
grossProfit = x.Sum(z => z.Field<Decimal>("grossProfit")),
traffic = x.Sum(z => z.Field<int>("traffic")),
boxes = x.Sum(z => z.Field<int>("boxes")),
monitored = x.Key.monitored
}).PropertiesToDataTable(groupdatatable);
Here My class Which I have created
public class groupdatatable
{
public string CompanyName { get; set; }
public Int32 POS { get; set; }
public string storeName { get; set; }
public Int32 SpecialIdentifier { get; set; }
public string EmployeeName { get; set; }
public DateTime processDate { get; set; }
public string dow { get; set; }
public string monthsName { get; set; }
public Int32 years { get; set; }
public Int32 MonthDay { get; set; }
public Int32 processHour { get; set; }
public Decimal grossProfit { get; set; }
public Int32 traffic { get; set; }
public Int32 boxes { get; set; }
public string monitored { get; set; }
}
but it giving me error can you check the code.
public static DataTable PropertiesToDataTable<T>(this IEnumerable<T> source)
{
DataTable dt = new DataTable();
var props = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in props)
{
DataColumn dc = dt.Columns.Add(prop.Name, prop.PropertyType);
dc.Caption = prop.DisplayName;
dc.ReadOnly = prop.IsReadOnly;
}
foreach (T item in source)
{
DataRow dr = dt.NewRow();
foreach (PropertyDescriptor prop in props)
{
dr[prop.Name] = prop.GetValue(item);
}
dt.Rows.Add(dr);
}
return dt;
}
解决方案
推荐阅读
- postgresql - 创建聚合initcond中的postgresql格式错误的数组文字
- python - Python:通过 Flask-socketio 获取队列并发出消息,这可以用 While True 吗?
- typescript - 完全扩展联合类型
- git - 错误用户的 Git 403
- powershell - Powershell一旦锁定就不会发送密钥
- angular - 当我运行命令“ng build”时,我无法从浏览器输入路线,只能通过内部导航
- javascript - 有没有办法强制 XMLHttpRequest 使用 HTTP/1.1?
- kubernetes - Helm rabbitmq 端点不存在
- amazon-web-services - 尝试在 Route 53 中自动注册我的 EC2 实例
- sql - TOAD DB2 Like 运算符