gpt4 book ai didi

c# - 嵌套分组策略/算法 c#

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:23 25 4
gpt4 key购买 nike

不确定我是否必须为此类问题更正标签,但您对以下问题的通用解决方案有任何想法吗?

给定发票集合:

var invoices = new List<Invoice>()
{
new Invoice() { Id = 1, Customer = "a", Date = DateTime.Parse("1/1/2009") },
new Invoice() { Id = 2, Customer = "a", Date = DateTime.Parse("1/2/2009") },
new Invoice() { Id = 3, Customer = "a", Date = DateTime.Parse("1/2/2009") },
new Invoice() { Id = 4, Customer = "b", Date = DateTime.Parse("1/1/2009") },
new Invoice() { Id = 5, Customer = "b", Date = DateTime.Parse("1/1/2009") },
new Invoice() { Id = 6, Customer = "b", Date = DateTime.Parse("1/2/2009") }
}

我可以在指定类似内容的地方使用什么技术:

var tree = invoices.ToHeirarchy(t => {
t.GroupBy(x => x.Date);
t.GroupBy(x => x.Customer);
})

结果如下图:

Date "1/1/2009"
Customer a
Invoice 1
Customer b
Invoice 4
Invoice 5
Date "1/2/2009"
Customer a
Invoice 2
Invoice 3
Customer b
Invoice 6

并且还允许以下通过(允许对任何级别的发票进行计算)?

Assert.AreEqual(3, tree.Node[0].Items.Count)
Assert.AreEqual(DateTime.Parse("1/1/2009"), tree.Node[0].Key)

Assert.AreEqual(3, tree.Node[1].Items.Count)
Assert.AreEqual(DateTime.Parse("1/2/2009"), tree.Node[1].Key)

Assert.AreEqual("a", tree.Node[0].Node[0].Key)
Assert.AreEqual(1, tree.Node[0].Node[0].Items.Count)

Assert.AreEqual("b", tree.Node[0].Node[1].Key)
Assert.AreEqual(2, tree.Node[0].Node[1].Items.Count)

最佳答案

您要查找的术语是“嵌套的 groupby”。

http://msdn.microsoft.com/en-us/vcsharp/aa336754.aspx#nested

关于c# - 嵌套分组策略/算法 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1009963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com