简体   繁体   中英

LINQ Convert IEnumerable<string> to List<ulong>

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?

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?

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();
var personIds = (from player in myPlayers select Convert.ToUInt64(player.person_id)).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM