C#使用带和的元组
我有这张桌子:C#使用带和的元组,c#,tuples,C#,Tuples,我有这张桌子: 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
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
我希望:
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
这意味着集团的NBR_COLIS限额为10,NBR_COLIS上的平均POID
我的代码:
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>";
}
List oListTUP=new List();
while(readerOne.Read())
{
Tuple oTup=new Tuple(readerOne[“CODE_DEST”].ToString(),Convert.ToInt32(readerOne[“NBR_COLIS”]),Convert.ToDecimal(readerOne[“POID”]);
合并(oTup);
}
var结果=oListTUP
.GroupBy(x=>x.Item1)
.选择(g=>new
{
键=g键,
Sum=g.Sum(x=>x.Item2),
Poids=g.Sum(x=>x.Item3),
})
.选择(p=>new
{
键=p.键,
项目=可枚举。重复(10,p.总和/10)
.Concat(可枚举重复(p.Sum%10,1)),
CalculPoids=p.poid/Items.Count
})
.SelectMany(p=>p.Items.Select(i=>Tuple.Create(p.Key,i,p.CalculPoids)))
.ToList();
foreach(结果中的var oItem)
{
Label1.Text+=oItem.Item1+“--”+oItem.Item2+“--”+oItem.Item3+”
”;
}
我对不能安静工作的POID有问题
问题是:CalculPoids=p.poid/Items.Count
我发现我的项目。计数始终为0我发现:
CalculPoids = p.Poids/(Enumerable.Repeat(10, p.Sum / 10).Concat(Enumerable.Repeat(p.Sum % 10, 1))).Count()
你可能应该把它改写成一个问题而不是一个陈述。