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();