0

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());
    }
}
3
  • what is Bar? is it a class or what is it? Commented Jun 10, 2010 at 6:17
  • Good catch, fixed. A class obviously. Commented Jun 10, 2010 at 6:22
  • I didn't think it was obvious :) I was stumped actually ..lol. Anyways time to goto bed... Commented Jun 10, 2010 at 6:25

2 Answers 2

2

This one's simple, if I've understood you rightly:

List<Foo> foos = gloop.Foos; // Or wherever you're getting it from
List<Bar> bars = foos.Select(foo => foo.Bar)
                     .ToList();

If you only need an IEnumerable<Bar> you can just use Select without the call to ToList. Of course you don't need the foos local variable if you don't want it - you can just have a single statement. I've only separated them out in case you've got an existing List<Foo> or Foo[] (you mention arrays in your subject line).

Sign up to request clarification or add additional context in comments.

11 Comments

What's more efficient, the Select with lambda, or something like (from b in gloop.Foos select b.Bar).ToList()
@Mystere Man: They end up as the same IL, but I don't see any reason for using a query expression when you're just doing a simple projection. It's extra clutter.
Since he mentioned Linq, the Linq equivalent for the second statement should be: List<Bar> bars = (from f in foos.Bar select f).ToList();
Good point Jon, as usual, you're the voice of reason and wisdom ;)
@mynameiscoffey: Why? LINQ doesn't necessitate the use of query expressions IMO. We're still using LINQ to Objects, but taking the simplest syntax for the situation, IMO. Not to mention the fact that your query wouldn't work as written ;)
|
1

var bars = gloop.Foos.Select(foo => foo.Bar);

3 Comments

As stated in another answer, this only returns IEnumerable<Bar>. Use ToList() if you need it as List<Bar>.
did you forget to add the "ToList()"? or is that the return type of the Select call?
Yes, I forgot the ToList() in my initial (quick) answer. IList adds some additional functionality to IEnumerable, like insert/delete which may be inappropriate given the source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.