总和可为 null 的 Linq 查询
-
22-08-2019 - |
题
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId
select v.Points).Sum()
}
我收到了这个查询,但是如果没有发现投票,则查询会失败:
The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
我认为它是因为 sum 返回一个 int 而不是一个可为 null 的 int,给 sum 一个 int?因为输入只给出相同的错误,可能导致 sum 只适用于整数。
有什么好的解决方法吗?
解决方案
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId
select v.Points ?? 0).Sum()
}
编辑 - 确定这个什么......(再次拍摄,因为我不知道你的模型...):
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId)
.Sum(v => v.Points)
}
其他提示
您想要使用 Sum 的可为 null 形式,因此请尝试将您的值转换为可为 null:
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId
select v.Points).Sum(r => (decimal?) r.Points)
}
此处更详细地讨论您的问题:
假设 “v.Points” 是一个小数只是使用下面的:
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId
select (decimal?) v.Points).Sum() ?? 0
}
如果你不喜欢铸造nullabe十进制你也可以使用LINQ to对象与ToList()方法试试,
空集合的LinqToObjects总和为0,其中空集合的LinqToSql萨姆为空。
尝试检查了这一点:
var count = db.Cart.Where(c => c.UserName == "Name").Sum(c => (int?)c.Count) ?? 0;
因此,问题的根源在于,SQL查询是这样的:
SELECT SUM([Votes].[Value])
FROM [dbo].[Votes] AS [Votes]
WHERE 1 = [Votes].[UserId]
返回NULL
有一个简单而有效的解决办法是唯一的总和,其中Points.Count> 0,所以你从来没有空值的票数:
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId &&
v.Points.Count > 0
select v.Points).Sum()
}
只需添加另一方法进入组合:)
Where(q=> q.ItemId == b.ItemId && b.Points.HasValue).Sum(q=>q.Points.Value)
我也有类似的情况,但我没有比较其他字段总结时...
Where(q => q.FinalValue.HasValue).Sum(q=>q.FinalValue.Value);
假设点被的Int32,如何像的列表:
var sum = Points.DefaultIfEmpty().Sum(c => (Int32)c ?? 0)
我有同样的问题。与空列表联合解决它:
List<int> emptyPoints = new List<int>() { 0 };
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Votes
where b.ItemId == v.ItemId
select v.Points).Union(emptyPoints).Sum()
}
在“点”的情况下是整数这应该工作。
我认为这是相同的情况下。 我解决它。这是我的解决方案:
var x = (from a in this.db.Pohybs
let sum = (from p in a.Pohybs
where p.PohybTyp.Vydej == true
select p.PocetJednotek).Sum()
where a.IDDil == IDDil && a.PohybTyp.Vydej == false
&& ( ((int?)sum??0) < a.PocetJednotek)
select a);
我希望这帮助。
(from i in Db.Items
where (from v in Db.Votes
where i.ItemId == v.ItemId
select v.Points).Count() > 0
select new VotedItem
{
ItemId = i.ItemId,
Points = (from v in Db.Items
where i.ItemId == v.ItemId
select v.Points).Sum()
}).Union(from i in Db.Items
where (from v in Db.Votes
where i.ItemId == v.ItemId
select v.Points).Count() == 0
select new VotedItem
{
ItemId = i.ItemId,
Points = 0
}).OrderBy(i => i.Points);
这个工作,但不是很漂亮或可读的。
我也有类似的问题,并得到什么,我试图摆脱数据库的解决方案上来,做那些计数,然后只有当我有什么做回一笔。无法得到剧组因某种原因使这种张贴如果其他人有类似问题的工作。
e.g。
Votes = (from v in Db.Votes
where b.ItemId = v.ItemId
select v)
然后检查一下,如果你有这样你就不会得到空返回任何结果。
If (Votes.Count > 0) Then
Points = Votes.Sum(Function(v) v.Points)
End If
以前的答案相似,但你也可以施放整个总和的结果可空类型。
from i in Db.Items
select new VotedItem
{
ItemId = i.ItemId,
Points = (decimal?)((from v in Db.Votes
where b.ItemId == v.ItemId
select v.Points).Sum()) ?? 0
}
可以说这更好的适应究竟怎么回事,但它在这个答案投同样的效果。
我以为我会抛出另一种解决方案在那里。我也有类似的问题,这是我的最后解决它:
Where(a => a.ItemId == b.ItemId && !b.IsPointsNull()).Sum(b => b.Points)