将字符串集合转换为GUID集合 [英] Convert a Collection of Strings into Collection of GUID

查看:99
本文介绍了将字符串集合转换为GUID集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 excel 中读取一个字符串(GUID格式)集合,并希望将它们作为 GUID集合存储在数据库中.我只是想知道是否有任何干净的方法可以使用 Linq List< string> 转换为 List< Guid> .我对循环不感兴趣.

I am reading a collection of strings (of GUID format) from excel and want to store them in database as collection of GUIDs. I am just wondering is there any clean way to convert List<string> to List<Guid> using Linq. I am not interested in looping through.

推荐答案

任一LINQ:

List<Guid> guids = guidStrings.Select(Guid.Parse).ToList();

List.ConvertAll ,效率更高,因为它知道大小:

or List.ConvertAll which is a little bit more efficient because it knows the size:

List<Guid> guids = guidStrings.ConvertAll(Guid.Parse);

这篇关于将字符串集合转换为GUID集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆