将List<string>转换为Model类

huangapple go评论71阅读模式
英文:

Convert List<string> to Model class

问题

在C#中将List<string>转换为Student对象时遇到问题。

我有一个如下所示的C#模型类:

public class Student{
   public int ID{get;set;}
   public string StudentName{get; set;}
   public string StudentAddress{get; set;}
   public string StudentRemarks{get; set;}
   public string AdditionalInfo{get;set;}
}

我有另一个类,其中有一个List<string>,其中包含以下数据(由于这只是一个字符串列表,它不会有任何属性名称在其前面,例如'ID: 001')注意:此字符串不会包含任何关于'AdditionalInfo'的数据。

001
John, Snow
NewYork
Sample test info

现在我有另一个类,我想将这个List<string>转换为我的'Student'类,其中001必须分配给ID,John Snow必须分配给'StudentName',NewYork必须分配给'StudentAddress',Sample test info必须分配给'StudentRemarks'。由于这没有提供关于'AdditionalInfo'属性的任何数据,因此应该将其分配为空或null值。以下是该类:

public class StudentInfoService
{
    public List&lt;string&gt; GetStudentInfo(string data)
    {
        var studentData = new List&lt;string&gt;();

        foreach (var line in File.ReadLines(data))
        {
            if (!string.IsNullOrWhiteSpace(line))
            {
                var data = line.Split('|');
                foreach (var item in data)
                {
                    studentData.Add(item);
                }
                studentData.ConvertAll(c => (Student)c); //这是我在尝试将字符串列表转换为模型类时遇到困难的地方
            }
        }
        return studentData;
    }
}

问题是,我想将List<string>转换为'Student'对象,并按顺序自动将所有数据分配给相应的属性(除了'AdditionalInfo'之外,中间不会有空值或空数据)。Student对象将只有一条学生记录。它不会有多条记录的列表。因此,我需要将List<string>转换为Student对象。请帮助我解决这个问题。

英文:

Having issue with converting List<string> to Student object in c#.

I have a C# model class as below:

public class Student{
   public int ID{get;set;}
   public string StudentName{get; set;}
   public string StudentAddress{get; set;}
   public string StudentRemarks{get; set;}
   public string AdditionalInfo{get;set;}
}

I have another class where I have a List<string> which holds data as below (Since this is just a list of string, it won't have any property names in front of it such as 'ID: 001') Note: This string will not have any data for 'AdditionalInfo'.

001
John, Snow
NewYork
Sample test info

Now I have another class where I wanted to convert this List<string> to my 'Student' class where 001 has to be assigned to ID, John Snow has to be assigned to 'StudentName', NewYork has to be assigned to 'StudentAddress', Sample test info has to be assigned to 'StudentRemarks'. Since this doesn't have any data provided for 'AdditionalInfo' property, this should be assigned with empty or null value in it. Here is the class

public class StudentInfoService
{
    public List&lt;string&gt; GetStudentInfo(string data)
    {
        var studentData = new List&lt;string&gt;();

        foreach (var line in File.ReadLines(data))
        {
            if (!string.IsNullOrWhiteSpace(line))
            {
                var data = line.Split(&#39;|&#39;);
                foreach (var item in data)
                {
                    studentData.Add(item);
                }
                studentData.ConvertAll(c =&gt; (Student)c); //Here is where I am struggling to convert the list string to model class
            }
        }
        return studentData ;
    }
}

The issue is, I want to convert the list<string> to 'Student' object and automatically assign all the data to the respective properties by order(there won't be any null or empty data in between other than the 'AdditionalInfo'). Student object will have only one student record. It won't have a list of records. So I need to convert the List<string> to Student object. Please help me with this.

答案1

得分: 1

你需要编写代码将文本行映射到模型实例,例如

public Student GetStudent(List<string> list)
{
    return new Student
    {
        ID = int.Parse(list[0]),
        StudentName = list[1],
        StudentAddress = list[2],
        StudentRemarks = list[3],
        AdditionalInfo = (list.Count > 4) ? list[4] : null
    };
}
英文:

You will need to write code to map lines of text to a model instance, e.g.

public Student GetStudent(List&lt;string&gt; list)
{
    return new Student
    {
        ID = int.Parse(list[0]),
        StudentName = list[1],
        StudentAddress = list[2],
        StudentRemarks = list[3],
        AdditionalInfo = (list.Count &gt; 4) ? list[4] : null
    };
}

huangapple
  • 本文由 发表于 2023年2月14日 07:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定