1

I want to create a list which will hold two int values lets say it arrfirst and another list that will hold lists of arrfirst, lets say it arrsecond then check if arrsecond contains a defined arrfirst . Now i created lists but when i try to use .Contains it doesn't work.

List<List<int>> Eklenenler = new List<List<int>>();

for (int i = 0; i < YourTable.Rows.Count; i++)
        {
            prjAd = YourTable.Rows[i].Field<string>("F1");
            if (prjAd != "Servis Adresi")
            {
                wesAd = YourTable.Rows[i].Field<string>("F2");
                RowPrj = tbProjeler.AsEnumerable().Where(r => r.Field<string>("prjAd") == prjAd).FirstOrDefault();
                RowWes = tbServisler.AsEnumerable().Where(k => k.Field<string>("wesAd") == wesAd).FirstOrDefault();
                prjId = RowPrj.Field<int>("prjId");
                wesId = RowWes.Field<int>("wesId");
                List<int> temp = new List<int>();
                temp.Add(prjId);
                temp.Add(wesId);
                if (!Eklenenler.Contains(temp))
                {
                    sqlExProjeEkle = "INSERT INTO NLK_PRJ_Webservisler (prjId,wesId) Values (" + prjId + "," + wesId + ")";
                    dco.Execute(sqlExProjeEkle);
                    Eklenenler.Add(temp);

                }
                else
                {

                }
            }                                                  
        }

This is my code. When i debugging, i can see temp values are right and also at second loop, temp is in Eklenenler list but it always goes in if block. For example, first loop shows temp[0]=36 and temp[1]=42 goes in if block and at second loop again temp[0]=36 and temp[1]=42 also Eklenenler[0][0]=36 and Eklenenler[0][1]=42 so it contains temp but it still goes in. How can i solve this problem and why this returns false and can't find this temp in Eklenenler. Thank you.

1
  • 1
    if(row.SequenceEquals(anotherRow)) ?
    – mrogal.ski
    Commented Mar 31, 2017 at 14:24

2 Answers 2

6

You could do it with linq in just one line with something like this

if (!Eklenenler.Any(c => c.SequenceEqual(temp)))
{
    sqlExProjeEkle = "INSERT INTO NLK_PRJ_Webservisler (prjId,wesId) Values (" + prjId + "," + wesId + ")";
    dco.Execute(sqlExProjeEkle);
    Eklenenler.Add(temp);

}
else
{

}

Don't forget to add

using System.Linq;
0
0

Here's an answer using a foreach loop.

foreach (List<int> list in Eklenenler)
{
    if (list.SequenceEqual(temp))
    {
        //Do work
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.