0

I have a ListBox where table names are written like this:

Staging_Section_01_2019_03_19_01  
Staging_Section_01_2019_03_20_01  
Staging_Section_23_2019_03_21_01  
Staging_Section_52_2019_03_23_01  
Staging_Section_52_2019_03_24_01  

What I am trying to do is to separate them by Section Number, so I want all Section_01 in one List object and Section_23 in another List object, so on and so forth. The dynamic nature is whats making it difficult for me.

So far, I have the following:

foreach (var it in uploadBox.Items)
{
    if (it.ToString().Contains("Section"))
    {
        section = it.ToString().Substring(0, 18);
        found = it.ToString().IndexOf("_");
        section = section.Substring(found + 1);
        sectionNum = section.Substring(8, 2);
    }
}

I have gotten the sectionNum which would just be the number and section, which is the string like Section_01.

Any idea on how to approach this?

The expected output would be something like this:

List 1

Staging_Section_01_2019_03_19_01  
Staging_Section_01_2019_03_20_01  

List 2

Staging_Section_23_2019_03_21_01  

List 3

Staging_Section_52_2019_03_23_01  
Staging_Section_52_2019_03_24_01  
6
  • 2
    This looks like a great fit for regular expressions. Commented Apr 10, 2019 at 19:50
  • 1
    I think the op's question is how to put them is separate lists by their number. Commented Apr 10, 2019 at 19:54
  • 1
    Please show us an example of what is your expected output Commented Apr 10, 2019 at 19:55
  • 1
    And, is it possible to have a Section_100 or a Section_1000? Commented Apr 10, 2019 at 20:01
  • is it always Staging_Section_##?. If so, just store the current one and on the next loop if that substring is not the same as your stored one, create a new list and add the current one. Each time saving to ensure that you are comparing the previous for a change. Commented Apr 10, 2019 at 20:02

3 Answers 3

1

I would use a Dictionary<string, List<string>> for this. Each 'section' that is parsed would be a key, and the remaining portion would the the value.

Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>();
foreach (var it in uploadBox.Items)
{
    if (it.ToString().Contains("Section"))
    {
        section = it.ToString().Substring(0, 18);
        found = it.ToString().IndexOf("_");
        section = section.Substring(found + 1);
        sectionNum = section.Substring(8, 2);

        if(!myDict.ContainsKey(sectionNum))
        {
            myDict.Add(sectionNum, new List<string> { someOtherValue });
        }
        else
        {
            myDict[sectionNum].Add(someOtherValue);
        }
    }
}

Unless I have completely misinterpreted your question, I think this is a potential solution to your dynamic objects.

Sign up to request clarification or add additional context in comments.

2 Comments

That Substring(0,18) will be a problem if we have a ...Section_100...
I agree. I'd recommend splitting on '_' and just joining index 1 and 2.
0

you could do something like this:

var sections = new Dictionary<string, List<string>>();

foreach(var it in uploadBox.Items)
{
    var item = it.ToString();

    if(item.Contains("Section"))
    {

        var section = GetSection(item);

        if(!sections.ContainsKey(section))
        {
            sections.Add(section, new List<string>());            
        } 

        sections[section].Add(item);
    }    
}

private string GetSection(string item)
{
    var split = item.Split("_");
    return $"{split[1]}_{split[2]}";    
}

Comments

0

It is best to regex for this kind of task:

uploadBox.Items
    .GroupBy(x => Regex.Match(x.ToString(), @"^\w+_Section_(?<section>\d+)").Groups["section"].Value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.