0

I have this following Code in C# using EF Core 1.2 where I'm reading the input of a textarea and check each line if it matches one of my patterns. After checking one line I'm trying to set a state which tells me if it matches one pattern.

Now I would like to save these lines temporary plus each of its state into a list so I can pass this list to my view, where I want to display each line plus its state in a table.

My problem is how do I save each line plus its state? I thought about using a Dictionary but I'm not sure if this is the best solution for my problem.

using (StringReader reader = new StringReader(Request.Form["ExpressionTextarea"].ToString()))
{
    string line = string.Empty;

    do
    {
        line = reader.ReadLine();

        if (line != null)
        {
            string state = CheckStringLine(line);

            /**** HOW TO SAVE EACH LINE PLUS ITS STATE TEMPORARILY?
            //IDictionary<string, string> dictionary = new Dictionary<string, string>();
            //dictionary.Add(line, status);
            ****/
        }
    } while (line != null);

    //***PASSING MY LIST TO MY VIEW
    return View(MYLIST);
}

//Checks if line matches a pattern
public string CheckStringLine(string Line)
{          
    string state = "";
    //Pattern1: (Ein | Eine) A ist (ein | eine) B.
    string pattern1 = @"^(?<Artikel1>(Ein|Eine){1})\s{1}(?<Second>[A-Z]{1}[a-zäöüß]{1,})\s{1}ist\s{1}(?<Artikel2>(eine|ein){1})\s(?<Fourth>[A-Z]{1}[a-zäöüß]{1,})\.$";

    //Pattern2: (Ein | Eine) A (oder (ein | eine) B)+ ist (ein | eine) C.
    string pattern2 = @"^(?<First>(Ein|Eine){1})\s{1}(?<Second>[A-Z]{1}[a-zäöüß]{1,})(\s{1}oder\s{1}(?<OptionalArtikel>(ein|eine){1})\s{1}(?<OptionalBegriff>[A-Z]{1}[a-zäöüß]{1,}))+(\s{1})ist\s{1}(?<Third>(eine|ein){1})\s(?<Fourth>[A-Z]{1}[a-zäöüß]{1,})\.$";

    var match1 = Regex.Match(Line, pattern1);
    var match2 = Regex.Match(Line, pattern2);

    if (match1.Success)
    {
        state = "This Line is using pattern1";

        return state;
    }
    if (match2.Success)
    {
        state = "This Line is using pattern2";
        return state;
    }           

    state = "No matches";
    return state; 
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
Joe Jonsman
  • 103
  • 1
  • 6

2 Answers2

2

Using a Dictionary is for situations where you want a key/value pair and I understand here you just want to store things. Also if you want to keep the order it wouldn't work.

The easiest way is to use a List containing a Tuple, if you don't want to create your own type for this.

var list = new List<Tuple<string, string>>();
list.Add(new Tuple<string, string>(line, state));

Then when you need them you can get the tuples from the list and get the line from Item1 and the state from Item2.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
0

You can user any in-memory data structure to store your state. HashTable will be much faster than other data structures but it is not generic. You can use Dictionary as well, which is generic.

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43