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

coma separated string to List object

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

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 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;
    }

>Solution :

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],
    }; 
}  
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