C# using Tuple with Sum [closed]

一个人想着一个人 提交于 2019-12-13 11:28:55

问题


I have this table:

CODE_DEST | NBR_COLIS | POIDS
-----------------------------
     a    |     6     | 2
     b    |     7     | 5
     c    |     1     | 1
     a    |     5     | 3
     a    |     3     | 1
     b    |     4     | 4
     g    |     2     | 4

I expexted:

     CODE_DEST | NBR_COLIS | POIDS
    -----------------------------
         a    |     10    | 3
         a    |     4     | 3
         b    |     10    | 4,5
         b    |     1     | 4,5
         c    |     1     | 1
         g    |     2     | 4

that meant group by NBR_COLIS LIMIT by 10 and AVG POIDS on NBR_COLIS

my code:

List<Tuple<string, int, decimal>> oListTUP = new List<Tuple<string, int, decimal>>();
                while (readerOne.Read())
                {
                    Tuple<string, int, decimal> oTup = new Tuple<string, int, decimal>(readerOne["CODE_DEST"].ToString(), Convert.ToInt32(readerOne["NBR_COLIS"]), Convert.ToDecimal(readerOne["POID"]));
                    oListTUP.Add(oTup);

                }

                var result = oListTUP
                    .GroupBy(x => x.Item1)
                    .Select(g => new
                    {
                        Key = g.Key,
                        Sum = g.Sum(x => x.Item2),
                        Poids = g.Sum(x => x.Item3),
                    })
                    .Select(p => new
                    {
                        Key = p.Key,
                        Items = Enumerable.Repeat(10, p.Sum / 10)
                                          .Concat(Enumerable.Repeat(p.Sum % 10, 1)),
                        CalculPoids = p.Poids / Items.Count

                    })
                    .SelectMany(p => p.Items.Select(i => Tuple.Create(p.Key, i, p.CalculPoids)))
                    .ToList();

                foreach (var oItem in result)
                {
                    Label1.Text += oItem.Item1 + "--" + oItem.Item2 + "--" + oItem.Item3 + "<br>";
                }

I have the problem with POIDS that not work quietly.

The problem is : CalculPoids = p.Poids / Items.Count

I found that my Items.Count always 0


回答1:


i found:

 CalculPoids = p.Poids/(Enumerable.Repeat(10, p.Sum / 10).Concat(Enumerable.Repeat(p.Sum % 10, 1))).Count()


来源:https://stackoverflow.com/questions/8248579/c-sharp-using-tuple-with-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!