Home > Back-end >  coma separated string to List object
coma separated string to List object

Time:06-22

I have a function that is using two for loops. This needs to run 100k times once for every row in a particular table. The data comes in in a string comma separated values. I need to turn this data into a Bag Object. I have a working solution below However, I was wondering if there is a faster approach to this problem.

Data comes in like this.

string bagList="2C28DB,737,2C28DB,738,2C28DB,739,2C28DB,740,2C28DB,741,2C28DB,742,2C28DB,743,2C28DB,744,2C28DB,745,2C28DB,746,2C28DB,747,2C28DB,748,2C28DB,749,2C28DB,750,2C28DB,751,2C28DB,752,2C28DB,753,2C28DB,754,2C28DB,755,2C28DB,756,"

The BagModel looks like this

  public class BagsModels
   {
    public string LotNumber { get; set; }
    public string BagNumber { get; set; }
   }

I use a split to get an array then load the odds in one list and evens in another list. From here I create a bag model object and load both lists into this and add it to the final list. I use the if statement not sure if the last comma will create an empty value in the array. The string always contains pairs. There will never be a case where it does not have a lot number and bag number together. Any help would be appreciated.

internal static IEnumerable<BagsModels> ListifyTheString(string bagList)
    {
        List<BagsModels> temp = new();
        string[] temp2 = bagList.Split(',');
        List<string> lotno = new();
        List<string> bagno = new();
        for(int i = 0; i < temp2.Length; i  )
        {
            if(temp2[i] != "")
            {
                if(i%2 == 0)
                {
                    lotno.Add(temp2[i]);
                }
                else
                {
                    bagno.Add(temp2[i]);
                }
            }
        }
        for(int j = 0; j < lotno.Count; j  )
        {
            BagsModels temp3 = new();
            temp3.LotNumber = lotno[j];
            temp3.BagNumber = bagno[j];
            temp.Add(temp3);
        }
        return temp;
    }

CodePudding user response:

You can slightly change for loop and get something like this (the last odd item if it exists will be ignored):

internal static IEnumerable<BagsModels> ListifyTheString(string bagList) {
  var items = bagList.Split(','); 

  for (int i = 0; i < items.Length / 2;   i) 
    yield return new BagsModels() {
      LotNumber = items[2 * i],
      BagNumber = items[2 * i   1],
    }; 
}  

CodePudding user response:

You can use the function like this. You don't need multiple loops you can do this in single loop also.

public static List<BagsModels> ConvertStringToBags(string requestString)
{
        List<BagsModels> result = new();
        var splittedString = requestString.Split(',');
        for(int idx=0; idx < splittedString.Length; idx = idx   2){
         result.Add(new(){
             LotNumber = splittedString[idx],
             BagNumber = ((idx   1) <= splittedString.Length-1) ? splittedString[idx 1] : string.Empty
         });
        }
        return result;
    }

CodePudding user response:

If you are looking for an optimized fast version of your code, you must avoid Split the string. Think about what Split do internally: create an array in memory with lots of items and this is an expensive operation if you run lots of times.

internal static IEnumerable<BagsModels> ListifyTheString(string bagList)
{
    var temp = new List<BagsModels>();

    var prevIndex = 0;
    var index1 = bagList.IndexOf(',');
    var index2 = bagList.IndexOf(',', index1   1);
    while (index2 >= 0)
    {
        var lotNumber = bagList.Substring(prevIndex, index1 - prevIndex);
        var bagNumber = bagList.Substring(index1   1, index2 - index1 - 1);

        temp.Add(new BagsModels
        {
            LotNumber = lotNumber, 
            BagNumber = bagNumber
        });

        prevIndex = index2   1;

        index1 = bagList.IndexOf(',', prevIndex);
        index2 = index1 < 0 ? -1 : bagList.IndexOf(',', index1   1);
    }

    if (index1 > 0)
    {
        var lotNumber = bagList.Substring(prevIndex, index1 - prevIndex);
        var bagNumber = bagList.Substring(index1   1);

        temp.Add(new BagsModels
        {
            LotNumber = lotNumber,
            BagNumber = bagNumber
        });
    }

    return temp;
}

Previous code is longer than other answers but is faster. It's only searching the next comma and extracting the substrings that you need.

  •  Tags:  
  • c#
  • Related