在LINQ查询异常处理 [英] Exception handling in Linq queries
本文介绍了在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.
所以,现在的IM搭售处理try-catch块这个查询里面。结果
请帮忙。
So now i m tying to handle Try-Catch block inside this query.
Please help.
推荐答案
在情况下,如果你只是想忽略坏分子,那么:
In case if you just want to ignore "bad elements" then:
App.lstAllChilds.SkipExceptions().Where( e => e.Id == Id).ToList();
扩展方法:
Extension method:
public static class Extensions
{
public static IEnumerable<T> SkipExceptions<T>(this IEnumerable<T> values)
{
using (var enumerator = values.GetEnumerator())
{
bool next = true;
while (next)
{
try
{
next = enumerator.MoveNext();
}
catch
{
continue;
}
if (next) yield return enumerator.Current;
}
}
}
}
这篇关于在LINQ查询异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文