Ian Vink
Ian Vink

Reputation: 68800

Linq: Getting a List<String> from List<Customer> based on customer.name

I have a List<Customer> and need to get a List<string> based on the Customer.Name field. This is for a picker UI.

How can I query a List for that?

Upvotes: 0

Views: 222

Answers (4)

CrackingWise
CrackingWise

Reputation: 103

Yet another syntax...

var customerNames = (from c in myCustomers select c.Name).ToList();

Upvotes: 0

Jonas
Jonas

Reputation: 4584

A quick test (which may be faulty) tells me this is a bit faster:

var customerNames = myCustomers.ConvertAll(c => c.Name);

At least it's another way to do the same thing.

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160982

var customerNames = myCustomers.Select( c=> c.Name).ToList();

Upvotes: 1

p.campbell
p.campbell

Reputation: 100607

Try this:

List<string> names = custs.Select(x=>x.Name).ToList();

Upvotes: 1

Related Questions