Aravind
Aravind

Reputation: 177

How to Search a String in List<Tuple<string,string>> in C#

I am having a

        List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
        tr.Add(new Tuple<string, string>("Test","Add");
        tr.Add(new Tuple<string, string>("Welcome","Update");

        foreach (var lst in tr)
         {
             if(lst.Contains("Test"))
              MessageBox.Show("Value Avail");

          }

I failed while doing this ,....

Upvotes: 14

Views: 38326

Answers (7)

Jackypengyu
Jackypengyu

Reputation: 261

List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");
var index = tr.FindIndex(s=>s.Item1 == "Test" || s.Item2 == "Test");
if(index != -1)
MessageBox.Show("Value Avail");

Using FindIndex, you can check the availability and index of element at the same time.

Upvotes: 2

SunsetQuest
SunsetQuest

Reputation: 8817

Maybe this might help someone else. Here is the method I went with:

List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add");
tr.Add(new Tuple<string, string>("Welcome","Update");

if(lst.Any(c => c.Item1.Contains("Test")))
    MessageBox.Show("Value Avail");

(credit goes here)

Upvotes: 1

horgh
horgh

Reputation: 18534

Probably this should work:

foreach (var lst in tr)
{        
    if (lst.Item1.Equals("Test"))        
        MessageBox.Show("Value Avail");
}

or this

if (lst.Item1.Equals("Test") || lst.Item2.Equals("Test"))

Read Tuple Class; you'll need to access values of the tuple through Item1 and/or Item2 properties.


And why use Tuple at all? Maybe this is easier:

Dictionary<string, string> dict = new Dictionary<string, string>
{
    {"Test", "Add"},
    {"Welcome", "Update"}
};

if (dict.ContainsKey("Test"))
{
    MessageBox.Show("Value Avail:\t"+dict["Test"]);
}

Upvotes: 10

jam40jeff
jam40jeff

Reputation: 2596

If you'd like to use LINQ:

if(tr.Any(t => t.Item1 == "Test" || t.Item2 == "Test"))
    MessageBox.Show("Value Avail");

This will also have the benefit of only showing the message box once if the text is found multiple times (if that is what is desired).

Upvotes: 16

Tilak
Tilak

Reputation: 30698

Change

if(lst.Contains("Test"))

To

 if(lst.Item1.Contains("Test") ||  lst.Item2.Contains("Test"))

If tuple has more items, you need to add condition for all items.

If you want to make it common for all tuples, you need to use Reflection (and the quirky way).

Upvotes: 0

boindiil
boindiil

Reputation: 5865

Why are you iterating over lstEvntType and not tr? You should try this:

List<Tuple<string,string>> tr = new List<Tuple<string,string>>();
tr.Add(new Tuple<string, string>("Test","Add"));
tr.Add(new Tuple<string, string>("Welcome","Update"));
List<Tuple<string,string>>  lstEvntType = new List<Tuple<string,string>>();

    foreach (var lst in tr)
    {
        if(lst.Item1.Equals("Test"))
            MessageBox.Show("Value Avail");
    }

Upvotes: 0

Ameen
Ameen

Reputation: 2586

it should be foreach (var lst in tr) not lstEvntType and you should test for the tuple's Item1 field instead.

Upvotes: 1

Related Questions