[英].SELECT statement using lambda and Entity Framework
I am struggling here in using .SELECT
statement. 我在使用
.SELECT
语句时遇到了.SELECT
。
Here a snippet of my code; 这是我的代码片段;
var payoutsPerLocation = locations.Select(l => l.Payouts);
foreach (var payouts in payoutsPerLocation)
{
Console.WriteLine(payouts.Sum(pos=>pos.Amount));
}
Where in location is a list, and one location has one or more payout. 位置中有一个列表,一个位置中有一个或多个支出。 I want to get all the payouts.
我想得到所有的支出。
The above code's result is below 上面的代码结果如下
0
0
0
0
0
0
It should be the sum of the amount of payouts per location. 它应该是每个位置的支出金额之和。 It seems like I don't get any records of payout.
看来我没有任何付款记录。 But when I tried using the code below (for testing if I am getting payout);
但是,当我尝试使用下面的代码(用于测试是否获得付款)时;
var payoutsPerLocation = locations.FirstOrDefault().Payouts;
foreach (var payout in payoutsPerLocation)
{
Console.WriteLine(payout.Amout);
}
There is a payout for that location. 该位置有支出。 What do you guys think I am doing wrong here?
你们认为我在这里做错了什么?
Any help is appreciated. 任何帮助表示赞赏。
I don't know why it's giving you zeros, but you shouldn't evaluate Linq expressions inside a foreach loop anyway. 我不知道为什么会给您零,但是无论如何您都不应该在foreach循环中评估Linq表达式。 It causes too many round-trips to the database.
这会导致数据库往返过多。 Try this:
尝试这个:
var sumPerLocation = locations.Select(l => l.Payouts.Sum(p => p.Amount));
foreach (var sum in sumPerLocation)
{
Console.WriteLine(sum);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.