Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将字符串拆分为对象列表_C#_List - Fatal编程技术网

C# 如何将字符串拆分为对象列表

C# 如何将字符串拆分为对象列表,c#,list,C#,List,我有一个字符串“105,113312;105,213314;105,313316;106,113318;106,213320;106,313322” 我需要将字符串转换为列表 List<MyClass> registers = new List<MyClass>(); class MyClass{ public int TOT_ID; public int phaze; public int register; }

我有一个字符串
“105,113312;105,213314;105,313316;106,113318;106,213320;106,313322”

我需要将字符串转换为列表

List<MyClass> registers = new List<MyClass>();

class MyClass{
        public int TOT_ID;
        public int phaze;
        public int register;
    }
我犯了一个错误

Error   191 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)   
你可能忘了

using System.Linq;
然后:

registers = registersArr.Split(';')
                        .Select(x => x.Split(','))
                        .Where(x => x.Length == 3)
从这里更改:

                        .Select(x => new MyClass 
                               { 
                                    TOT_ID = int.Parse(x[0]), 
                                    phaze = int.Parse(x[1]), 
                                    register = int.Parse(x[2]) 
                               })
                        .ToList();
试试这个代码

 string s = "105,1,13312;105,2,13314;105,3,13316;106,1,13318;106,2,13320;106,3,13322";
        List<MyClass> registers = new List<MyClass>();
        registers = s.Split(';')
            .Select(x => new MyClass() {
                TOT_ID = x[0], 
                phaze = x[1],
                register =x[2] 
            }).ToList();
string s=“105,113312;105,213314;105,313316;106,113318;106,213320;106,313322”;
列表寄存器=新列表();
寄存器=s.Split(“;”)
.Select(x=>newmyclass(){
TOT_ID=x[0],
phaze=x[1],
寄存器=x[2]
}).ToList();

这对我来说很有用:

registers=registersArr.Split(“;”) .Select(x=>newmyclass{TOT_ID=int.Parse(x.Split(',')[0]),phaze=int.Parse(x.Split(',')[1]),register=int.Parse(x.Split(',')[2]))
.ToList()

使用
Cast
方法,因为
Select
方法用于
列表
,而不是
数组
在何处使用它,将其转换为什么?
Cast
,因为您有一个
字符串
数组。你有没有
使用System.Linq参考?
 string s = "105,1,13312;105,2,13314;105,3,13316;106,1,13318;106,2,13320;106,3,13322";
        List<MyClass> registers = new List<MyClass>();
        registers = s.Split(';')
            .Select(x => new MyClass() {
                TOT_ID = x[0], 
                phaze = x[1],
                register =x[2] 
            }).ToList();