c# - Exception handling within a LINQ Expression -
i have simple linq-expression like:
newdocs = (from doc in alldocs getdocument(doc.key) != null select doc).tolist();
the problem is, getdocument() throw exception. how can ignore doc-elements getdocument(doc.key) == null or throws exception?
the same code in old school looks like:
foreach (var doc in alldocs) { try { if (getdocument(doc.key) != null) newdocs.add(doc); } catch (exception) { //do nothing... } }
alldocs.where(doc => { try { return getdocument(doc.key) != null; } catch { return false; } }).tolist();
i'm not sure it's possible using query comprehension syntax, except via baroque atrocity this:
newdocs = (from doc in alldocs ((predicate<document>)(doc_ => { try { return getdocument(doc_.key) != null; } catch { return false; } }))(doc) select doc).tolist();
Comments
Post a Comment