-1

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.

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?

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • 2
    Should that not be new Root() with a capital R? – Svein Terje Gaup Dec 17 '21 at 10:34
  • Did you look at this: https://stackoverflow.com/questions/22870624/convert-json-string-to-json-object-c-sharp/22870885 – Svein Terje Gaup Dec 17 '21 at 10:36
  • Just use `.Split(';')` on the String object and then process the resulting array. This seems more like a question for Google than for StackOverflow. – Nyerguds Dec 17 '21 at 10:48
  • **Unrelated**: If you have control over the database, storing N report type codes in a single column smells bad, looks like your database could be normalized. – Cleptus Dec 17 '21 at 10:55

5 Answers5

4

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()
Roy Cohen
  • 1,540
  • 1
  • 5
  • 22
1

With the help of System.Linq you can do it quite easy, see below^^

using System.Linq;


new Root()
{
     name="Test",
     creationDateTime=Convert.ToDateTime("2021-11-08"),
     reportTypeCodes= "One;Two;Three"
           .Split(';')
           .Select(x => new ReportTypeCode(){codeValue = x})
           .ToList()
}
Isparia
  • 682
  • 5
  • 20
0

You can write it in the following way:

var Obj = new Root()
{
    name = "Test",
    creationDateTime = Convert.ToDateTime("2021-11-08"),    
   reportTypeCodes = new List<ReportTypeCode>(){
        new ReportTypeCode(){ codeValue = "one"},
        new ReportTypeCode(){ codeValue = "two" }
   }
};
Mario
  • 278
  • 2
  • 15
  • Hi Mario... for known length of strings we can write above but String Value we are not sure whether it may 3 or more. it comes from DB. so code wise we need to iterate. Can you pls help me for my case. – Kalyan Reddy Dec 17 '21 at 10:39
  • @KalyanReddy Then you need to write code to iterate these database results and make them into a list. That's unrelated to the code you showed though. `List` has a `.Add()` function exactly for that purpose. – Nyerguds Dec 17 '21 at 10:44
0

Depends a bit on what you want to do...

        var rootTest = new Root();

        rootTest.reportTypeCodes = new List<ReportTypeCode>()
        {
            new ReportTypeCode()
            {
                codeValue = "hello world"
            },
            new ReportTypeCode()
            {
                codeValue = "hello world 2"
            },
        };

        foreach(var code in rootTest.reportTypeCodes)
        {
            Console.WriteLine(code.codeValue);
        }
Wimpix
  • 11
  • 3
-1

I think you're looking for this:

Var Obj = new root(){
           name="Test",
           creationDateTime=Convert.ToDateTime("2021-11-08),
           reportTypeCodes= new List<ReportTypeCode>{
               new ReportTypeCode { codeValue = "A"; },
               new ReportTypeCode { codeValue = "B"; }
           }
    }
Eduard Keilholz
  • 820
  • 8
  • 27