首页 > 解决方案 > 如果您在 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;
      }

标签: c#

解决方案


推荐阅读