简体   繁体   English

使用lambda / linq语句进行异常处理

[英]Exception handling with lambda/linq statements

I have the following statement that I am trying to use a try-catch block with 我有以下声明,我正在尝试使用try-catch块

var val= db.shops.where(x=>×.id==ID).Select (a=>a.address).First();

I tried following but has many issues won't even compile. 我尝试了以下方法,但是有很多问题甚至无法编译。

var val=db.shops.Where(x=> 
  {
    try 
    {
        (x.Id==ID).Select(a=>a.address).First();
    }
    catch(ex)
    {
      return ex;
    }
  }

Please let me know how can I handle the exception in this statement Thanks. 请让我知道如何处理此语句中的异常谢谢。 Note: writing this question from mobile phone can't format code. 注意:用手机写这个问题不能格式化代码。 Apologize for it. 对此表示歉意。

Everything inside brackets ( { } ) need to have 'regular' block syntax, so return is required here: 方括号( { } )中的所有内容都必须具有“常规”块语法,因此此处需要return:

...
.Where
(x=> 
  {
    try 
    {
        return (x.Id == ID);
    }
    catch(ex)
    {
      throw;
    }
  }
)
.Select(a=>a.address)
.First(); // Or FirstOrDefault if you expect this statement to yield no result.

As you see, the Where is more like a regular statement now. 如您所见, Where现在更像是一条常规语句。 The Select is moved to outside the Where . Select将移到Where之外。 If you need exception handling in there, you have to do the same as in the Where block now. 如果在那里需要异常处理,则必须与现在的Where块相同。 Last, return ex is probably meant to be throw ex , which should be throw in this case to preserve call stack. 最后, return ex可能应该是throw ex ,在这种情况下应该throw该异常以保留调用堆栈。

...that I am trying to use try catch block with ...我正在尝试使用try catch block

I am guessing you probably meant to write FirstOrDefault based on where you want the try/catch . 我猜您可能打算根据想要try/catch位置编写FirstOrDefault In the sample code you have provided I see no reason to try to make a catch block into one of the lambda statements. 在您提供的示例代码中,我认为没有理由尝试将catch块放入一个lambda语句中。 The best thing to do would be to simply use FirstOrDefault as that is the reason why you might get an exception in the code shown. 最好的办法就是简单地使用FirstOrDefault,因为这就是您可能在所示代码中获得异常的原因。

var address = db.shops.FirstOrDefault(x => ×.id == ID)?.Address;
if(address == null)
  // nothing was found or address is null, do something else

Or similar code closer to what you had without my "optimization" 或类似的代码更接近于您在没有“优化”的情况下所拥有的代码

var shop = db.shops.FirstOrDefault(x => ×.id == ID);
if(shop == null) {
  // nothing was found, do something else
}
var address = shop.address;

The other reason not to use try/catch is that it can't be translated into SQL and the variable name db which is the container for the shops collection which leads me to believe you are using EF or some other ORM. 不使用try / catch的另一个原因是它无法转换为SQL,而变量名dbshops集合的容器,这使我相信您正在使用EF或其他ORM。 So even if you were to fix the syntax and your code with added try/catch block compiles you will get a run time error later when you execute the lambda. 因此,即使您要修复语法和添加了try / catch块编译的代码,稍后在执行lambda时也会遇到运行时错误。

You should place the try around your original statement: 您应该围绕原始语句进行try

try
{
   var val= db.shops.where(x=>×.id==ID).Select (a=>a.address).First();
}
catch (Exception ex)
{
   return ex; //I assume the return type here is wrong.  Maybe return null?
}

However, there's nothing about that line that should require a try/catch . 但是,该行没有任何内容需要try/catch Try this: 尝试这个:

var val= db.shops.where(x=>×.id==ID).Select(a=>a.address).FirstOrDefault();

The FirstOrDefault will return null if there are no results. 如果没有结果,则FirstOrDefault将返回null

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

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