Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 值不能为null。错误_C#_Wpf_Linq - Fatal编程技术网

C# 值不能为null。错误

C# 值不能为null。错误,c#,wpf,linq,C#,Wpf,Linq,下面的方法有一个错误,我试图将数据从组合框添加到数据网格,然后在添加数据后,我想计算“ItemSellingPrice”的总金额并在标签中显示该金额 using (TruckServiceClient TSC = new TruckServiceClient()) { var item = cmbAddExtras.SelectedItem as ExtraDisplayItems; if (item != null) { var displayItem

下面的方法有一个错误,我试图将数据从组合框添加到数据网格,然后在添加数据后,我想计算“ItemSellingPrice”的总金额并在标签中显示该金额

using (TruckServiceClient TSC = new TruckServiceClient())
{
    var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;

    if (item != null)
    {
        var displayItem = new List<ExtraDisplayItems>
        {
            new ExtraDisplayItems 
            { 
                displayItems = item.displayItems,
                ItemId = item.ItemId, 
                ItemCode = item.ItemCode, 
                ItemDescription = item.ItemDescription, 
                ItemSellingPrice = item.ItemSellingPrice,
            }
        };
        dgAddExtras.Items.Add(item);
        var subTotalExtras = item.displayItems.Sum(x => x.ItemSellingPrice.GetValueOrDefault(0)); //Here
        lblSubTotalExtrasAmount.Content = "R" + subTotalExtras;
    }
}
使用(TruckServiceClient TSC=new TruckServiceClient())
{
var item=cmbAddExtras.SelectedItem作为ExtraDisplayItems;
如果(项!=null)
{
var displayItem=新列表
{
新的ExtraDisplayItems
{ 
displayItems=item.displayItems,
ItemId=item.ItemId,
ItemCode=item.ItemCode,
ItemDescription=item.ItemDescription,
ItemSellingPrice=item.ItemSellingPrice,
}
};
dgAddExtras.Items.Add(item);
var subTotalExtras=item.displayItems.Sum(x=>x.ItemSellingPrice.GetValueOrDefault(0));//此处
lblSubTotalExtrasAmount.Content=“R”+小计;
}
}
我得到的错误是:

值不能为null

有人知道为什么会这样吗

编辑:这是我在类中设置displayItems的地方

public class ExtraDisplayItems
{
    public List<ExtraDisplayItems> displayItems;

    public int ItemId { get; set; }    
    public string ItemCode { get; set; }    
    public string ItemDescription { get; set; }    
    public double? ItemSellingPrice { get; set; }
}
公共类ExtraDisplayItems
{
公共物品清单;
公共int ItemId{get;set;}
公共字符串ItemCode{get;set;}
公共字符串ItemDescription{get;set;}
公共双项目SellingPrice{get;set;}
}
由于,实际的问题是
项.displayItems
null
(因此不在lambda中,因为这可以很好地工作)

一些证据。这将获取您的确切错误消息:

double?[] x = null;
double? sum = x.Sum(y => y);
(您没有得到
NullReferenceException
的原因是您实际上调用了一个静态方法(扩展方法),因此从技术上讲,引用不是空的,但参数是空的)


您应该检查设置
item.displayItems
的位置,或者禁止对
项调用
Sum
。displayItems
的值为
null

。但这不是答案。我仍然收到相同的错误?不,值不能为空。是当LINQ扩展方法的第一个参数为null时得到的消息,在本例中为
item.displayItems
。这是错误的。只是默默地跳过空值。@GertArnold确实如此。谢谢你的评论。修正了答案。是的,比我的答案早了四秒钟,所以我没有时间在添加我自己的评论之前查看他的评论。:)我现在觉得你的答案不错。