将字符串转换为模型类

时间:2019-09-04 04:22:17

标签: c# asp.net entity-framework

如何将以下字符串转换为模型类?

string temp = "Oliver/Christina=2373400019485 Ortosan/David=2373400019486"

数据输出:

FirstName = Christina
LastName = Oliver
Code = 2373400019485

FirstName = David
LastName = Ortosan
Code = 2373400019486

我的模型班:

public class Test
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Code { get; set; }
}

1 个答案:

答案 0 :(得分:1)

使用此:

string temp = "Oliver/Christina=2373400019485 Ortosan/David=2373400019486";
            var PersonstrList = temp.Split(' ');
            List<Test> PersonList = new List<Test>();
            foreach (var p in PersonstrList)
            {
                Test t = new Test();
                t.LastName = p.Substring(0,p.IndexOf("/"));
                t.FirstName = p.Substring(p.IndexOf("/"), p.IndexOf("/") - p.IndexOf("="));
                t.Code = p.Substring(p.IndexOf("="));
                PersonList.Add(t);
            }

首先按空格('')分割字符串,然后将每个数组元素用作一个人。通过子字符串,我们可以拆分每个人的属性。