Is there a neat way to ignore exceptions in Linq? I., let's say, I have a class ObjectAthat takes a string parameter in its constructor, and some verification happens inside the constructor - this means that if the string does not have the required format, the constructor will throw. With the following code, I would get a list ObjectAfrom 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 linq line, a la (pseudo code is suitable ...)
var result = ListOfFiles.Select(n => try {new ObjectA(n)}).ToList();
source
share