我有一个字符串"105,1,13312;105,2,13314;105,3,13316;106,1,13318;106,2,13320;106,3,13322"
我需要将字符串转换为列表
List<MyClass> registers = new List<MyClass>();
class MyClass{
public int TOT_ID;
public int phaze;
public int register;
}
我试过
registers = registersArr.Split(';')
.Select(x => x.Split(','))
.Where(x => x.Length == 3)
.ToList(x => x[0], x => x[1], x=> x[2]);
我收到错误
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?)
答案 0 :(得分:2)
你可能忘记了
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();
答案 1 :(得分:0)
试试此代码
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();
答案 2 :(得分:0)
这对我有用:
registers = registersArr.Split(&#39 ;;&#39;) .Select(x =&gt; new MyClass {TOT_ID = int.Parse(x.Split(&#39;,&#39;)[0]),phaze = int.Parse(x.Split(&#39;, &#39;)[1]),register = int.Parse(x.Split(&#39;,&#39;)[2])}) .ToList();