-2

My EF query is supposed to be sorting by the date of the first Product in the list, but for some reason, it only sorts most of the products and some of the dates are in the wrong order.

Here's the code...

using (var context = new SalesEntities())
{
   var groupedData = context.s84_Schedule.AsExpandable()
      .Where(predicate)
      .GroupBy(c => new { c.CustomerID, c.s84_Customer.CustomerName, c.SubdivisionID, c.s84_Subdivision.SubdivisionName, c.LotNumber })
      .Select(grouped => new s84_Report_Project_POCO
      {
         CustomerID = grouped.Key.CustomerID,
         CustomerName = grouped.Key.CustomerName,
         SubdivisionID = grouped.Key.SubdivisionID,
         SubdivisionName = grouped.Key.SubdivisionName,
         LotNumber = grouped.Key.LotNumber,
         Products = grouped.Select(x => new s84_Report_Project_Product
         {
            ProductID = x.ProductID,
            ProductName = x.s84_Product.ProductName,
            ProductDate = x.CustomerExpectedDate,
            FieldRepID = x.FieldRepID,
            FieldRepName = x.s84_FieldRep.FieldRepName,
            InstallerID = x.InstallerID,
            InstallerName = x.s84_Installer.InstallerName,
            StatusID = x.StatusID,
            StatusColor = x.s84_Status.StatusColor,
            StatusName = x.s84_Status.StatusName,
            Completed = x.Completed
         }).ToList()
      });

   var finalList = groupedData.ToList().Where(x => x.Products.Last().Completed == false).ToList();

   List<s84_Report_Project_POCO> lst = finalList.OrderBy(x => x.Products.First().ProductDate).ToList();
   return lst;
}

Code seems good to me, but look at how one of the dates is out of order...

weird sorting http://www.84sales.com/weird_sort.png

4

3 回答 3

1

尝试在初始选择上进行排序

var groupedData = context.s84_Schedule.AsExpandable()
                              .Where(predicate)
                              .GroupBy(c => new { c.CustomerID,
                                                  c.s84_Customer.CustomerName, 
                                                  c.SubdivisionID, 
                                                  c.s84_Subdivision.SubdivisionName, 
                                                  c.LotNumber })
                              .Select(grouped => new s84_Report_Project_POCO
                              {
                                  CustomerID = grouped.Key.CustomerID,
                                  CustomerName = grouped.Key.CustomerName,
                                  SubdivisionID = grouped.Key.SubdivisionID,
                                  SubdivisionName = grouped.Key.SubdivisionName,
                                  LotNumber = grouped.Key.LotNumber,
                                  Products = grouped
                                    .Select(x => new s84_Report_Project_Product
                                  {
                                      ProductID = x.ProductID,
                                      ProductName = x.s84_Product.ProductName,
                                      ProductDate = x.CustomerExpectedDate,
                                      FieldRepID = x.FieldRepID,
                                      FieldRepName = x.s84_FieldRep.FieldRepName,
                                      InstallerID = x.InstallerID,
                                      InstallerName = x.s84_Installer.InstallerName,
                                      StatusID = x.StatusID,
                                      StatusColor = x.s84_Status.StatusColor,
                                      StatusName = x.s84_Status.StatusName,
                                      Completed = x.Completed
                                  }).OrderBy(x => x.CustomerExpectedDate).ToList()
                              });
于 2015-11-18T16:29:24.253 回答
1

问题在于.First()函数,女巫返回第一条记录,但不一定按日期顺序。如果您要按日期对分组数据进行排序,以便First()函数返回最近的日期,则需要在分组之前对数据进行排序,然后使用First()函数重新排序结果:

using (var context = PrimaryConnection.returnNewConnection())
        {
            var groupedData = context.s84_Schedule.AsExpandable()
                              .Where(predicate)
                              .GroupBy(c => new { c.CustomerID, c.s84_Customer.CustomerName, c.SubdivisionID, c.s84_Subdivision.SubdivisionName, c.LotNumber })
                              .Select(grouped => new s84_Report_Project_POCO
                              {
                                  CustomerID = grouped.Key.CustomerID,
                                  CustomerName = grouped.Key.CustomerName,
                                  SubdivisionID = grouped.Key.SubdivisionID,
                                  SubdivisionName = grouped.Key.SubdivisionName,
                                  LotNumber = grouped.Key.LotNumber,
                                  Products = grouped
                                    .Select(x => new s84_Report_Project_Product
                                  {
                                      ProductID = x.ProductID,
                                      ProductName = x.s84_Product.ProductName,
                                      ProductDate = x.CustomerExpectedDate,
                                      FieldRepID = x.FieldRepID,
                                      FieldRepName = x.s84_FieldRep.FieldRepName,
                                      InstallerID = x.InstallerID,
                                      InstallerName = x.s84_Installer.InstallerName,
                                      StatusID = x.StatusID,
                                      StatusColor = x.s84_Status.StatusColor,
                                      StatusName = x.s84_Status.StatusName,
                                      Completed = x.Completed
                                  }).Orderby(t => t.CustomerExpectedDate).ToList()
                              });

            var finalList = groupedData.ToList().Where(x => x.Products.Last().Completed == false).ToList();

            List<s84_Report_Project_POCO> lst = finalList.OrderBy(x => x.Products.First().ProductDate).ToList();
于 2015-11-18T16:43:12.753 回答
1

所有 SQL 查询(以及因此附加到 SQL 数据库时的 Linq 查询)具有随机顺序,除非您对它们进行排序。

产品未排序 - 因此它具有随机顺序。您按 Products.First() 排序,但 Products 具有随机顺序,因此您的排序也将是随机的。

确保产品在查询中排序,你应该没问题。

  Products = grouped.Select(....)
                    .OrderBy(x => x.ProductDate)
                    .ToList()
于 2015-11-18T16:52:09.677 回答