加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用TryParse的整数列表的整数字符串

发布时间:2020-12-15 18:03:23 所属栏目:百科 来源:网络整理
导读:我有这段代码: TheString = "443,432,546,4547,4445,2,132"; //actually,about 1000 entries Listint TheListOfIDs = new Listint(); TheListOfLeadIDs = from string s in TheString.Split(',') select Convert.ToInt32(s)).ToListint(); 我知道我可以使用
我有这段代码:
TheString = "443,432,546,4547,4445,2,132"; //actually,about 1000 entries    
List<int> TheListOfIDs = new List<int>();   
TheListOfLeadIDs = from string s in TheString.Split(',')
                   select Convert.ToInt32(s)).ToList<int>();

我知道我可以使用try catch来确保转换不会引发错误,但我想知道如何在linq语句中使用TryParse来完成这项工作.

谢谢.

解决方法

TheListOfLeadIDs = (from string s in TheString.Split(',')
                    let value = 0
                    where int.TryParse(s,out value)
                    select value).ToList<int>();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读