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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -