简体   繁体   English

Linq to SQL空值检查计算

[英]Linq to sql null value check in calculation

I have function which calculates quantity .Its working fine but some due to null values in field it does not return anything . 我有一个计算数量的函数。它可以正常工作,但由于字段中的空值而有些不返回任何东西。 these field can be null .i want to calculate only where both fields are not null. 这些字段可以为null。我只想计算两个字段都不为null的地方。 i think i need to use foreach but i dont know the way how can i use it here . 我认为我需要使用foreach,但是我不知道如何在这里使用它。 Help appreciated 感谢帮助

 private double Get_Quantity() 
    {
        try
        {
            double sum = 0;

            sum = (double)(from s in ManagerClass.oSqlData.table
                           where s.Demand.Order.Order_ID == Order_ID 
                           where s.Demand.Order.Order_Date >= ManagerClass.startDate && s.Demand.Order.Order_Date <= ManagerClass.EndDate

                           select s.ValueA - s.ValueB ).Sum();
            return sum;
        }
        catch
        {
            return 0;
        }
    }

Try like this; 这样尝试;

        sum = (double)(from s in ManagerClass.oSqlData.table
            where (s.Demand != null && s.Demand.Order != null && s.Demand.Order.Order_ID == Order_ID)
            && (s.Demand != null && s.Demand.Order != null && s.Demand.Order.Order_Date >= ManagerClass.startDate && s.Demand.Order.Order_Date <= ManagerClass.EndDate)
                  && s.ValueA != null
                  && s.ValueB != null
            select s.ValueA - s.ValueB).DefaultIfEmpty(0).Sum();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM