Suppose I have a class
public class Foo
{
public Bar Bar { get; set; }
}
Then I have another class
public class Gloop
{
public List<Foo> Foos { get; set; }
}
What's the easiest way to get a List<Bar>
of Foo.Bars?
I'm using C# 4.0 and can use Linq if that is the best choice.
UPDATE:
Just for a little dose of reality, the reason for this is that I have a Windows Service class that contains an inner ServiceBase derived class as a property. So I end up with code like this:
public class Service
{
public ServiceBase InnerService { get; set; }
}
public class ServiceHost
{
private List<Service> services = new List<Service>();
static void Main()
{
// code to add services to the list
ServiceBase.Run(services.Select(service => service.InnerService).ToArray());
}
}