LINQ Sum()中的IsNull[英] IsNull in LINQ Sum()

本文是小编为大家收集整理的关于LINQ Sum()中的IsNull的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在 LINQ 中遇到 IsNull 问题:

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

p.LoadingsDetails.Sum(n => n.Quantity) 可能性 NULL.我想要这样的结果:

select Id, IsNull(Sum(Quantity),0) as Quantity from LoadingsDetails

我尝试过类似的方法:

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = p.LoadingDetails.First() == null ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

它返回错误:

<块引用>

System.NotSupportedException:方法"First"只能用作最终查询操作.考虑在而是这个实例.

我尝试过这样的事情:

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = (p.LoadingsDetails.Sum(n => n.Quantity) == DBNull.Value) ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = p.LoadingsDetails.First().Quantity == DBNull.Value ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

返回错误:

<块引用>

运算符"=="不能应用于"十进制"和"DBNull"类型的操作数

在此处输入图片描述

推荐答案

您可以在这里使用 Any() 来检查 LoadingDetails 是否有任何行,如果有则将 Quantity 与 LoadingDetails 相加:

RemainingQuantity = p.LoadingDetails.Any()  ? 
                p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) : 
                p.Quantity 

本文地址:https://www.itbaoku.cn/post/1557214.html

问题描述

I have problem with IsNull in LINQ :

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

p.LoadingsDetails.Sum(n => n.Quantity) possibility NULL. I want to have result like this :

select Id, IsNull(Sum(Quantity),0) as Quantity from LoadingsDetails

I have tried something like :

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = p.LoadingDetails.First() == null ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

It return errors :

System.NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead.

I have tried something like this :

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = (p.LoadingsDetails.Sum(n => n.Quantity) == DBNull.Value) ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

OR

db.WarehouseInputsDetails
    .Select(p => new WarehouseInputDetailsViewModel
    {
        Id = p.Id
        RemainingQuantity = p.LoadingsDetails.First().Quantity == DBNull.Value ? p.Quantity : p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity)
    }).AsQueryable();

Return Errors :

Operator '==' cannot be applied to operands of type 'decimal' and 'DBNull'

enter image description here

推荐答案

You can use Any() here which will check if there is any row for LoadingDetails, if yes then Sum the Quantity for LoadingDetails:

RemainingQuantity = p.LoadingDetails.Any()  ? 
                p.Quantity - p.LoadingsDetails.Sum(n => n.Quantity) : 
                p.Quantity 
相关标签/搜索