c#列表中基于日期的唯一数据

本文关键字:日期 唯一 数据 列表 于日期 | 更新日期: 2023-09-27 18:17:45

我有一个表,它按某道菜在该月每天的销售次数排序,但顺序是:

[Dishes]
Fecha                       |   Pdv    | Pla_ID  |  Quantity |  Price   | Total |
===============================================================================
2016-11-03 00:00:00.000        REST      65        4           50.00     200.00
2016-11-05 00:00:00.000        REST      65        1           50.00      50.00
2016-11-07 00:00:00.000        REST      65        7           50.00     350.00
2016-11-03 00:00:00.000        REST      70        6           100.00    600.00
2016-11-04 00:00:00.000        REST      70        7           100.00    700.00
2016-11-05 00:00:00.000        REST      70        1           100.00    100.00
2016-11-06 00:00:00.000        REST      70        3           100.00    300.00
2016-11-07 00:00:00.000        REST      70        1           100.00    100.00

我正在寻找一种方法将记录保存到列表中,但日期是一个月的每一天的一列,并且数量值被分配到他们像这样出售的那一天按此顺序:

[Result]
|PDV|Pla_Id|Price|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|Quantity_Total|Total| 
========================================================================================================================
|REST| 65  |  50 |0|0|4|0|1|0|7|0|0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0|       12      | 600 |
|REST| 70  |  50 |0|0|6|7|1|3|1|0|0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0| 0|       18      | 1800 |

" Pla_Id = 65 "11月3日卖出4次,5日卖出1次,7日卖出7次,11月12次

" Pla_id = 70 "11月3日卖出6次,4日卖出7次,5日卖出1次,6日卖出3次,7日卖出1次,11月共卖出18次

所以我的列表是这样的:

["REST","65","50","0","0","4","0","1","0","7","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","12","$600.00"]
["REST","70","100","0","0","6","7","1","3","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","18","$1800.00"]

这是我目前的方法

foreach (var item in db.Dishes)
        {
            List<string> listItem = new List<string>();
            decimal costo = 0;
            for (int i = 1; i <= 31; i++)
            {
                var value = 0;
                if (item.Fecha.Day == i) { value = (int)item.Quantity; costo = costo + item.Total; }
            }
            listItem.Add(item.Pdv);
            listItem.Add(item.Pla_ID);
            listItem.Add(item.Price);

            for (int i = 1; i <= 30; i++)
            {
                var value = 0;
                int month = item.Fecha.Month;
                if (item.Fecha.Day == i)
                {
                    value = (int)item.Quantity;  listItem.Add(value.ToString());
                }
                value = 0;
                listItem.Add(value.ToString());
            }
            listItem.Add((item.Quantity).ToString());
            listItem.Add(item.price * item.Quantity);
        }

然而,这给了我每天分配给每个菜的数量,但在不同的行中,给了我3行"pl_id =65"的结果行,和5行"pl_id =70"的结果行每个都有相应的一天的数量,但每个不同的菜应该只有1行而不是每个不同的日子,

我怎样才能做到这一点?

c#列表中基于日期的唯一数据

我创建了以下模型来回答这个问题:

//主模型Dishes类

public class Dishes
{
    public DateTime Fecha { get; set;}
    public string Pdv { get; set;}
    public int Pla_ID { get; set;}
    public int Quantity { get; set;}
    public double Price { get; set;}
    public double Total { get; set;}    
}

//处理逻辑

  • 根据PdvPla_IdPrice字段创建分组,如下所示:

      var dishesGrouping = 
      dishes.GroupBy(d => new {d.Pdv,d.Pla_ID,d.Price});
    

基于Price对于Pdv, Pla_Id的组合也将保持不变的理解,正如我在您的数据中看到的那样,否则价格需要移出关键,并将成为日明智结构的一部分,如数量。

  • 现在创建以下DataStructure

    var dishGroupingDictionary = dishesGrouping
                             .ToDictionary(dishGrp => dishGrp.Key, dishGrp =>
                             {
                                var dishDictionary = new Dictionary<int, int>();
                                for(int i=1; i<=30;i++)
                                   dishDictionary[i] = 0;
                                 foreach (var grp in dishGrp)
                                 {
                                    dishDictionary[grp.Fecha.Day] = 
                                    grp.Quantity;
                                 }
                                return dishDictionary;
                             });
    
  • 创建的数据格式为:Dictionary<Anonymous(string,int,double),Dictionary<int,int>>

  • 现在你有了一个结构,将所有需要进一步处理的信息提供了一个例子:

  • 外字典有一个键作为Pdv, Pla_IDPrice的组合,这是所需的唯一组合

  • 内部字典包含Quantity的Day Wise数据,所有日期都填充0默认值,并更新为每个组密钥中的可用日期。

  • 现在每个Key-Value对将包含Pdv, Pla_IDPrice的唯一组合,每日明智的数量可以通过内部字典添加,价格可以乘以总数。剩余天数默认为0

//下面是简单的数据打印逻辑。您甚至可以计划将其存储在不同的和更平坦的model中以便快速使用,但它更倾向于将Day wise数据保存在像Dictionary这样的集合中,因为它易于扩展和修改,这对于model来说并不简单。

foreach (var kv in dishGroupingDictionary)
{
    Console.WriteLine("Pdv:{0},Pla_Id:    
    {1},Price{2}",kv.Key.Pdv,kv.Key.Pla_ID,kv.Key.Price);
    int totalQuantity = kv.Value.Sum(x => x.Value);
    Console.WriteLine("Total Quantity: {0}", totalQuantity);
    double totalPrice = totalQuantity * kv.Key.Price;
    Console.WriteLine("Total Price: {0}", totalPrice);
  // Day wise data printing:        
  foreach(var kvChild in kv.Value)
    Console.WriteLine("Day:{0},Quantity:{1}",kvChild.Key,kvChild.Value);        
}

首先创建一个表示您想要显示的数据的视图模型

public class DishVM
{
    public string Pdv { get; set; }
    public int Pla_ID { get; set; }
    public decimal Price { get; set; }
    public List<int> Days { get; set; }
    public int TotalQuantity { get; set; }
    public decimal TotalPrice { get; set; }
    public IEnumerable<Dishes> Data { get; set; }
}

假设您查询特定月份/年份的数据,为了使其更灵活,将该月中的天数分配给一个变量,例如int daysInMonth(使用DateTime.DaysInMonth()方法)。

生成视图模型的集合

var data = db.Dishes.Where(...).ToList(); // your query
int daysInMonth = 31; // hard coded for November 2016
// Group the data and initialize the collection of days for each group
var model = data.GroupBy(x => new { Pdv = x.Pdv, Pla_ID = x.Pla_ID, Price = x.Price })
    .Select(x => new DishVM()
    {
        Pdv = x.Key.Pdv,
        Pla_ID = x.Key.Pla_ID,
        Price = x.Key.Price,
        Days = new List<int>(new int[daysInMonth]),
        Data = x
    }).ToList();
// Assign the quantity for each day
foreach (var record in model)
{
    foreach(var dish in record.Data)
    {  
        int index = dish.Fecha.Day - 1;
        record.Days[index] = dish.Quantity;
        record.TotalQuantity += dish.Quantity;
    }
    record.TotalPrice = record.TotalQuantity * record.Price;
}