Hi,
I have a base class called LabFileBase. I have constructed a List and have added my derived classes to it. I want to search the List for a particular object based on a key I've defined. The problem I'm having is how do you downcast in a LINQ expression?
Here is some sample code:
public abstract class LabFileBase
{
}
public class Sample1 : LabFileBase
{
public string ID {get;set;}
public string Name {get;set;}
//..
}
public class Sample2 : LabFileBase
{
public string ID {get;set;}
public string Name {get;set;}
//..
}
I want to search for a particular Sample2 type, but I need to downcast if i used a regular foreach loop like this:
foreach(var s in processedFiles) //processedFiles is a List<LabFileBase>
if (s is Sample2)
var found = s as Sample2;
if (found.ID = ID && found.Name == "Thing I'm looking for")
//do extra work
I would much rather have something like this:
var result = processedFiles.Select(s => s.ID == SomeID && s.Name == SomeName);
Is this possible and what syntactic punctuation is involved, or is the foreach
my only option because of the different objects. Sample1 and Sample2 only have ID and Name as the same fields.
EDIT: Thanks to all for your support and suggestions, I've entered almost everything into the backlog to implement.