Implement static IEnumerable<T> function to skip Exception when run a query.
This function return valid items only.
- public static class LINQException {
- public static void TryExample() {
- var LsWithEx = from p in Enumerable.Range(0, 10) select int.Parse("dsfksdj");
- var LsSkipEx = (from p in Enumerable.Range(0, 10) select int.Parse("dsfksdj")).SkipExceptions();
- }
- 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;
- }
- }
- }
- }
I hope this help
Nessun commento:
Posta un commento