views:

63

answers:

3

I have the following code:

var personIds = from player in myPlayers select player.person_id;

where personIds is an IEnumerable<string> that I'd like to convert to List<ulong>, since person_id is convertable via Convert.ToUInt64()

Is this easily done in LINQ?

+2  A: 

You can do it like this:

var personIds = from player in myPlayers select player.person_id;
List<ulong> result = personIds.Select(id => Convert.ToUInt64(id)).ToList();
Nick Craver
+1 Exactly what I would do.
Oded
+8  A: 

Rather than convert your existing personIds, I'd do it in one query:

var personIds = myPlayers.Select(player => Convert.ToUInt64(player.person_id))
                         .ToList();

At least, I'd do that unless you also needed the IEnumerable<string> for something else, in which case you could use Nick's answer.

I'd also see whether you could change the type of person_id... if it's always a text representation of a ulong, why is it a string in the first place?

Jon Skeet
+1 - If this is an option always do it in one hop.
Nick Craver
A: 
var personIds = (from player in myPlayers select Convert.ToUInt64(player.person_id)).ToList();
Joel Coehoorn