Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#_Linq_Lambda - Fatal编程技术网

C# 在访问属性或调用方法之前检查null结果

C# 在访问属性或调用方法之前检查null结果,c#,linq,lambda,C#,Linq,Lambda,我想知道在访问C#Linq或lambda表达式中的属性或调用方法之前,是否有更好的方法或一般规则来检查null结果。提前谢谢 据我所知,为了避免“对象引用未设置为对象实例”或“在访问CSharp属性或调用方法之前从Lambda结果中检查null”等异常,我应该使用“if”语句或“try/catch”块,如: var product1 = _myProductRepository.FindOne(p => p.Id == -20301); //1. use "if" statement

我想知道在访问C#Linq或lambda表达式中的属性或调用方法之前,是否有更好的方法或一般规则来检查null结果。提前谢谢

据我所知,为了避免“对象引用未设置为对象实例”或“在访问CSharp属性或调用方法之前从Lambda结果中检查null”等异常,我应该使用“if”语句或“try/catch”块,如:

 var product1 = _myProductRepository.FindOne(p => p.Id == -20301); 

//1. use "if" statement to let code flow continue

if(product1 != null) // to prevent exception "Object reference not set to an instance of an object."
{
      int id1 = sh1.Id; // note: accessing property "Id" is safe here
      Console.WriteLine("Product Id: {0}", id1);

}


//2. use "try/catch" block to let code flow continue

var product2 = _myProductRepository.FindOne(123);

var possibleItems  = product2.Orders.Where(x => x.Id == -1);

List<Order> myOrderList = null;

try
{
       myOrderList = possibleItems.ToList(); // note: ToList() method call is safe here
}
catch
{
       myOrderList = new List<Order>();
}
var product1=\u myProductRepository.FindOne(p=>p.Id==-20301);
//1. 使用“if”语句让代码继续流动
if(product1!=null)//为了防止异常,“对象引用未设置为对象的实例。”
{
int id1=sh1.Id;//注意:在这里访问属性“Id”是安全的
WriteLine(“产品Id:{0}”,id1);
}
//2. 使用“try/catch”块让代码流继续
var product2=_myProductRepository.FindOne(123);
var possibleItems=product2.Orders.Where(x=>x.Id==-1);
列表myOrderList=null;
尝试
{
myOrderList=possibleItems.ToList();//注意:ToList()方法调用在这里是安全的
}
抓住
{
myOrderList=新列表();
}
我应该使用“if”语句或“try/catch”块,如:

 var product1 = _myProductRepository.FindOne(p => p.Id == -20301); 

//1. use "if" statement to let code flow continue

if(product1 != null) // to prevent exception "Object reference not set to an instance of an object."
{
      int id1 = sh1.Id; // note: accessing property "Id" is safe here
      Console.WriteLine("Product Id: {0}", id1);

}


//2. use "try/catch" block to let code flow continue

var product2 = _myProductRepository.FindOne(123);

var possibleItems  = product2.Orders.Where(x => x.Id == -1);

List<Order> myOrderList = null;

try
{
       myOrderList = possibleItems.ToList(); // note: ToList() method call is safe here
}
catch
{
       myOrderList = new List<Order>();
}
一般来说,我更喜欢显式的
null
检查
null
返回是否是应用程序正常操作的一部分

在您的案例中,这可能是正确的,因为找到一个产品似乎是一个很可能失败的操作,因为一个产品可能不存在


这就是说,如果ID是一个已知元素,并且确实应该始终在DB中,那么
null
将不是预期的结果。在这种情况下,
FindOne
方法确实不应该返回
null
实例,这确实是一种例外情况,那么可能会首选异常处理。

如果不存在任何异常,Where子句不会返回空列表吗?你需要试一试吗?同意。异常处理应该与正常运行的业务逻辑完全分离。