0

I created an array of mysql data. I want to compare each element of my array with the value entered in the textbox and print the result, but it doesn't work the way I want.Any idea of doing so?

**edit My goal is to actually make a guessing game, so I created two arrays with Mysql data called answers and questions. And what I want to do is take the value from the user and if it is true, for example my first answer 'fashion' matches the guess the user entered in the textbox, I want the label to write correct and continue with the next answer and try to find the next answer. **

        private void button1_Click(object sender, EventArgs e)

    {
        List<string> cevaplar = new List<string>();
        List<string> sorular = new List<string>();
        

        MySqlCommand command = new MySqlCommand("Select cevap,soru From soru_cevap", con.cn);

        con.cn.Open();

        MySqlDataReader oku = command.ExecuteReader();
        while (oku.Read())
        {

            var cevap = oku.GetString(0);
            var soru = oku.GetString(1);

            cevaplar.Add(cevap);
            sorular.Add(soru);

        }

        con.cn.Close();

        for(int i=0;i<cevaplar.Count;i++)
        {
            string tahmin = textBox1.Text;
            if (cevaplar.Any(item => item == tahmin))
            {
                label1.Text= "true";
                continue;

            }
            else
            {
                label1.Text = "false";
                break;
            }
        }


    }
naz naz
  • 11
  • 3
  • What is the type of cevaplar[i]? – Just Shadow Apr 13 '21 at 12:42
  • `var matches = cevaplar.Contains(tahmin);` – mjwills Apr 13 '21 at 12:42
  • Its type of string – naz naz Apr 13 '21 at 12:42
  • 1
    The first `false` will negate any `true` results and set the value of the textbox and bail out of the loop – Jonesopolis Apr 13 '21 at 12:46
  • You have an array you go through but only one label. So your code will always display "false" if there is at least one element that doesn't match. This doesn't fit your description of "compare each element and print the result". So it's unlcear what you actually want. What is the intended result? – TToni Apr 13 '21 at 12:47
  • Can you give a sample list of "cevaplar" and the required output for various "tahmin" values? – Hans Kesting Apr 13 '21 at 12:49
  • I guess you want to `break` if you have found a match not if there is no match. You could initialize a `bool` in the loop to `true` if you have found it. After the loop you check if it's true or false and set the `label1.Text` accordingly. Of course there are easier LINQ ways. – Tim Schmelter Apr 13 '21 at 13:02
  • Answers also have string values, so words are the same as questions, but I haven't used the questions yet. – naz naz Apr 13 '21 at 13:14
  • https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-5.0 – mjwills Apr 13 '21 at 14:03

1 Answers1

4

You can achieve your result by using the LINQ method Any() or Contains() (which will handle the looping for you) like this:

string tahmin = textBox1.Text;
// label1.Text = cevaplar.Any(item => item == tahmin) ? "true" : "false"
label1.Text = cevaplar.Contains(tahmin) ? "true" : "false"

Notes:

  • Don't forget to add using System.Linq to the top of your file if you use Any().
  • If you want the check to be case-insensitive you can use Any with item.Equals(tahmin, StringComparison.OrdinalIgnoreCase) instead of item == tahmin
  • The faster solution will still be Contains. But it doesn't allow lambdas, so the point above will not work there.
  • Any vs Contains discussion can found here
Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • What is the benefit of `Any` vs `Contains`? – mjwills Apr 13 '21 at 12:57
  • Actually there are differences there: https://stackoverflow.com/questions/4445219/linq-ring-any-vs-contains-for-huge-collections . That was a nice suggestion. Will update the answer. – Just Shadow Apr 13 '21 at 13:00
  • I know the differences. I am asking you what the benefit of `Any` vs `Contains` is here. I'll be more explicit - I think your answer could be improved by using `Contains`. – mjwills Apr 13 '21 at 13:01
  • 2
    If you want to check for a specific condition, use Any. If you want to check for the existence of an element, use Contains. – sameer Ahmed Apr 13 '21 at 13:01
  • Thank you for your answer. I tried and it says true when I enter the values ​​in the array, and false when I enter a value other than the array. But what I want is that it should be in order, that the 1st value of the array will be the first answer, the second value should be the second answer. – naz naz Apr 13 '21 at 13:12
  • I've tried both of them but they don't get in order. do you have an idea? – naz naz Apr 13 '21 at 13:39
  • `Enumerable.SequenceEqual`. – mjwills Apr 13 '21 at 14:03
  • but also comparing their length – naz naz Apr 13 '21 at 15:06
  • Did you try Enumerable.SequenceEqual @naznaz? – mjwills Apr 13 '21 at 23:14
  • @naznaz, So, you mean you have a few textboxes and labels, and for the textbox0 the answer is the cevaplar[0], for textbox1 it's cevaplar[1],... Did I get it right? – Just Shadow Apr 14 '21 at 08:01