0

I am trying to loop through 2 lists using this code

foreach (string str1 in list<string>tags)
{ 
    foreach (string str2 in list<string>ArchivedInformation) { }
}

The first list has tags which is stored as "tag1, tag2,tag3.." and the second list has information stored as "tag1, Datestamp, 0.01","tag2, datestamp, 0.02" and so on.

I would like to ask how can I get the Tags in the second list and use it as a condition to my first for each? I have tried splitting the second list but i am cannot get the exact "Tag1" as an id to use it as a condition.

In the end the goal I would want to do is Str1(from tags list) == Str2(From Archivedinformation).

5
  • 4
    Have you considered using a Dictionary? Commented Sep 26, 2014 at 17:58
  • 1
    I Haven't, thanks for the advice. Commented Sep 26, 2014 at 18:00
  • 1
    Just Want to know, is this doable using just only Dictionary? or it can be done using a list as well Commented Sep 26, 2014 at 18:06
  • 1
    Are you familiar with Dictionaries? It's a data structure that contains a list of keys and associated values. I would structure your data so the tags are dictionary keys and the ArchivedInformation values are the dictionary values. You wouldn't need to have the tag in the value, so it would just be like "Datestamp, 0.01". You could use a Dictionary<string, List<string>>. I'm not really sure I understand what you are actually trying to do with your foreach loop, though. It seems like you are just trying to associate the data, and if you weren't using two lists you might not even need to. Commented Sep 26, 2014 at 18:08
  • 1
    Yes i am actually accessing 2 lists at the same time as it is the current design of the system. but definitely a dictionary is something i need to look at its just that their preference is to access 2 lists and compare the id's on both of them. much thanks! Commented Sep 26, 2014 at 18:19

1 Answer 1

1

Everything's possible. Other thing is if it's even sane :)

    public void Something()
    {
        // Using Dictionary
        var dict = new Dictionary<string, string>();
        dict.Add("tag1", "tag1,datestamp,0.01");
        dict.Add("tag2", "tag2,datestamp,0.02");

        // out -> "tag2,datestamp,0.02"            
        System.Diagnostics.Debug.WriteLine(dict["tag2"]);    


        // Using two separate lists
        var tags = new List<string> { "tag1", "tag2" };
        var infos = new List<string> { "tag1,datestamp,0.01", "tag2,datestamp,0.02" };

        // out ->  tag1,datestamp,0.01 & tag2,datestamp,0.02
        tags.ForEach(tag =>
            System.Diagnostics.Debug.WriteLine(
                infos.First(info => info.StartsWith(tag)))); 
    }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! i tried to use this function as well string cmp; cmp = str2.Substring(0,str2.IndexOf(','));

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.