簡體   English   中英

基於日期的具有唯一數據的C#列表

[英]C# List with Unique data based on the Date

我有一個表,該表按當月某天每天售出菜的次數排序,但順序如下:

[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月中售出了4次,11月3日為1次,第五次為1次,第七次為7次,7次

“ Pla_id = 70”在11月的11月3日售出6次,第四次售出7次,第五次售出1次,第六次售出3次,第六次售出7次,在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條結果線表示“ Pla_Id = 65”,而5行對“ Pla_Id = 70”表示它們在相應的日期都有數量,但是應該每個不同的菜只有1行,而不是每天不同,

我該如何完成呢?

首先創建一個表示您要顯示的數據的視圖模型

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;
}

我創建了以下模型來回答這個問題:

//主要模型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}); 

基於這樣的理解,如我在您的數據中看到的Pla_Id ,對於PdvPla_Id的組合, Price也將保持不變,否則價格需要移出密鑰,並且將成為按日計算結構的一部分,例如數量。

  • 現在創建以下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>>

  • 現在您有了一個結構,將為進一步處理所需的所有信息提供一個示例:

  • 外字典有一個密鑰,它是PdvPla_IDPrice的組合,這是所需的唯一組合

  • 內部詞典包含“ Quantity日間明智”數據,所有日期均默認填充為0,並針對每個組密鑰中的可用日期進行更新。

  • 現在,每個Key-Value對將包含PdvPla_IDPrice的唯一組合, Pdv通過內部Dictionary添加按天數量,並且可以將Price乘以總計。 默認情況下,所有剩余天數應為0。

//以下是簡單的數據打印邏輯。 您甚至可以計划將其存儲在其他更平坦的model以便快速使用,但是它更傾向於將Day明智的數據保存在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);        

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM