我是一个.NET中的老蜜蜂,但Linq很新!经过一些基本的阅读后,我决定检查我的技能,我完全失败了!我不知道我在哪里犯错。在每个组中过滤后选择排名前N记录
我要选择最高2为了让每个人的金额,而100%== 0,
这里是我的代码。
var crecords = new[] {
new {
Name = "XYZ",
Orders = new[]
{
new { OrderId = 1, Amount = 340 },
new { OrderId = 2, Amount = 100 },
new { OrderId = 3, Amount = 200 }
}
},
new {
Name = "ABC",
Orders = new[]
{
new { OrderId = 11, Amount = 900 },
new { OrderId = 12, Amount = 800 },
new { OrderId = 13, Amount = 700 }
}
}
};
var result = crecords
.OrderBy(record => record.Name)
.ForEach
(
person => person.Orders
.Where(order => order.Amount % 100 == 0)
.OrderByDescending(t => t.Amount)
.Take(2)
);
foreach (var record in result)
{
Console.WriteLine(record.Name);
foreach (var order in record.Orders)
{
Console.WriteLine("-->" + order.Amount.ToString());
}
}
任何人都可以关注并告诉我什么是正确的查询?
在此先感谢
啊,这是正确的。非常感谢你。 – 2013-03-26 01:10:21
没问题 - 很高兴我能帮到:) – 2013-03-26 01:11:20
打我也是这样,这个错误的原因是由于ForEach()返回void因为它实际上修改自己,所以它不需要返回任何东西。所以你的'结果'将是空的 – Corylulu 2013-03-26 01:11:57