Is there a neat way to ignore exceptions in Linq? I.e., lets say I have a class ObjectA
that takes a string parameter in its constructor, and within the constructor there is some validation going on - meaning that if the string does not have the correct format, the constructor will throw. By the following code I would get a List of ObjectA
from a list of strings:
var result = new List<ObjectA>();
foreach (string _s in ListOfFiles) {
try {
ObjectA _A = new ObjectA(_s);
result.Add(_A);
}
catch{}
}
So my question is: Is there a one line linq way, a la (pseudocode coming up ...)
var result = ListOfFiles.Select(n => try {new ObjectA(n)}).ToList();