列表中的任何值为null时为空引用。 [英] Null reference when any of the values in the list is null.

查看:165
本文介绍了列表中的任何值为null时为空引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



Hi, I have the following code:

public void Sort()
{

    Collection = (from pricing in Collection orderby pricing.Prices.Price select pricing).ToList();

}





问题是当列表中的任何值为时,我得到一个空引用错误null。



当所有值都不为空时,它的工作正常。



如何克服这个问题错误?



谢谢!



The problem is I get a null reference error when any of the values in the list is null.

It works just fine when all values are not null.

how do I overcome this error?

Thanks!

推荐答案

您可以排除这样的空值:



You can exclude the null values like this:

Collection = (
    from pricing in Collection 
    where pricing != null && pricing.Prices != null
    orderby pricing.Prices.Price 
    select pricing).ToList();


这篇关于列表中的任何值为null时为空引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆