Linq查询中的异常处理 [英] Exception handling in Linq queries

查看:134
本文介绍了Linq查询中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用...

tmpLst = (from e in App.lstAllChilds where e.Id == Id select e).ToList();

其中 lstAllChilds 是列表,其中包含一些损坏的数据也是如此。

where lstAllChilds is the list, which contains some corrupted data as well.

所以现在可以处理这个查询中的Try-Catch块。

请帮助。

So now i m tying to handle Try-Catch block inside this query.
Please help.

推荐答案

以下是简单的更改,可以简单地排除null或引起异常的项目。

Here is the simplest change you can make to simply exclude items which are null or are raising an exception.

tmpLst = App.lstAllChilds.Where(e =>
{
    try
    {
        return e != null && e.Id == Id;
    }
    catch
    {
        return false;
    }
}).ToList();

但在我看来,您可能应该调查并解决潜在的问题。这似乎不是一个需要例外的场景。

But in my opinion you probably should investigate and solve the underlying problem. This doesn't seem like a scenario where exceptions should be expected.

这篇关于Linq查询中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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