Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In C# i have a issue with List from returned string

I am receiving from the database a single string with this content

One;Two;Three

and I want to store those values in the reportTypeCodes property in the following class.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class ReportTypeCode
{
    public string codeValue { get; set; }
}

public class Root
{
    public string name { get; set; }
    public DateTime creationDateTime { get; set; }
    public List<ReportTypeCode> reportTypeCodes { get; set; }
}

This is my current attempt:

Var Obj = new Root(){
           name="Test",
           creationDateTime=Convert.ToDateTime("2021-11-08),
           reportTypeCodes=?? // Don't know how to assign it.
    }

How can I do it?

>Solution :

First you want to take your string and split it on every ';' character. To do this, use Value.Split(';').
Then you want to take each section and turn it into a ReportTypeCode. To do this, you can use the LINQ method Select like so: .Select(s => new ReportTypeCode { codeValue = s }).
Finally, you want to get a list, so you need to call .ToList() (it is another LINQ method).
All put together:

Value.Split(';').Select(s => new ReportTypeCode { codeValue = s }).ToList()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading