简体   繁体   中英

C# insert values from string[] into list of model

I have this List of type model List<SequenceModel> listSequenceModel which contains few properties, like

public class SequenceModel
{
   public int Sequence1 {get;set;}
   public int Sequence2 {get;set;}
   public int Sequence3 {get;set;}
   public int OrderNr {get;set;}
}

I am getting this string with "_" which I split and create a List of string

string[] szFilterParamsList2 = model.szFilterParams.Split('_');

An example of this string would be "sequence1a_sequence1b_sequence1c", but it can also be "_ sequence2b _", or "__sequence3" or also "sequence1 __" (spaces here are for stackoverflow format reason, ignore them). It means I can get a list with three non nullable elements, or any variation of it with nullables, but at least one element must have a value.

List1: [0] = "123"
       [1] = "432"
       [2] = "575"
List2: [0] = "123"
       [1] = ""
       [2] = ""
List3: [0] = ""
       [1] = "98"
       [2] = ""

and so on.

How can I assign these values form the string[] into the List<SequenceModel> listSequenceModel ? For the empty elements I will assign hardcoded -1, since the values should always be positive (if not null).

EDIT: SequenceModel is used in another model like

public class BarcodeModel
{
   public int Product {get;set;}
   public decimal Price {get;set;}
   public List<SequenceModel> BarcodeSequence {get;set;}
}

Could this help you using Linq?

class Program
{
    static void Main(string[] args)
    {
        SplitStrings();
    }

    private static string sequence1 = "123_456_789";
    private static string sequence2 = "123__";
    private static string sequence3 = "__789";

    static void SplitStrings()
    {
        string[] szFilterParamsList1 = sequence1.Split('_').Select(s => string.IsNullOrEmpty(s) ? "-1" : s).ToArray();
        string[] szFilterParamsList2 = sequence2.Split('_').Select(s => string.IsNullOrEmpty(s) ? "-1" : s).ToArray();
        string[] szFilterParamsList3 = sequence3.Split('_').Select(s => string.IsNullOrEmpty(s) ? "-1" : s).ToArray();

        PrintArray(szFilterParamsList1);
        PrintArray(szFilterParamsList2);
        PrintArray(szFilterParamsList3);
    }

    static void PrintArray(string[] strArray)
    {
        foreach (string item in strArray)
        {
            Console.WriteLine(item);
        }
    }
}

Output:

123
456
789

123
-1
-1

-1
-1
789

Assuming that the array will always have 3 items, I would do something like this:

// 0. Make the string "-1" if it has no value
private string NormalizeSequenceValue(string s){
    return string.IsNullOrEmpty(s)? "-1" : s;
}

// 1. Map the array to a SequenceModel
private SequenceModel ToSequenceModel(string [] array, int index){
    if(array.Length != 3)
        throw new ArgumentException("Array should always contain 3 elements")

    return new SequenceModel{
        Sequence1 = NormalizeSequenceValue(array[0]),
        Sequence2 = NormalizeSequenceValue(array[1]),
        Sequence3 = NormalizeSequenceValue(array[2]),
        OrderNr = index
    }
}

//2. Map a list of arrays to a list of SequenceModel
private List<SequenceModel> ToSequenceModels(List<string[]> rawSequences){
    return rawSequences.Select((value, index) => ToSequenceModel(value, index)).ToList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM