I had a code:
var result = BOBase.Search(Mapper.SourceBrowserType, searchCriteria);
var list = new List<IBO>(result.OfType<IBO>());
But I need to be able to add many elements in a list. I tried:
var list = new List<IBO>();
var result = BOBase.Search(Mapper.SourceBrowserType, searchCriteria);
list.Add((IBO)result.OfType<IBO>());
Which results in an error:
Unable to cast object of type '<OfTypeIterator>d__95`1[BO.IBO]' to type 'BO.IBO'.
How to resolve this?
OfTypewill return a collection: IEnumerable. The problem is that you try to cast this entire collection to a single element. Hence the error message.The cast is already performed by the
OfTypecall. You would need a different adding method. It is called AddRange.