简体   繁体   中英

How can i check if a files already exist in the List?

This is the code:

for (int i = 0; i < lightningsRegions.Count; i++)
{
    if (!pdf1.Lightnings.Contains(lightningsRegions[i]))
    {
        pdf1.Lightnings.Add(lightningsRegions[i]);
        break;
    }
}

Both Lists LightningsRegions and Lightnings are type. I want to check if the LightningsRegions already exist in Lightnings do not add it again. But i used a breakpoing on the: pdf1.Lightnings.Add(lightningsRegions[i]); it keeps going there and add the same index i chose.

In my program i have a new Form there i can selecte from a meny a range of numbers for example i see on the new Form: Lightning 0 Length 32 [41 - 73] i click on this and it's going to the breakpoint and add it once to the Lightnings List.

So now in the Lightnings List in index[0] i have: "Lightning 0 Length 32 [41 - 73]" Now if in the same menu i click on this Lightning 0 Length 32 [41 - 73] again it should not go and add it to the Lightnings List but it does.

In this new Form menu i have many strings like this: Lightning 0 Length 32 [41 - 73] But i want to make sure that if i click on it twice it will add it only once and if i click on another one wich is not the same it will add it.

But for some reason now when i click on it twice it's getting to the same breakpoint and add it over again.

Updated:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                item = listBox1.SelectedItem.ToString();
                this.f1.PlayLightnings();
                f1.pdftoolsmenu();
            }
        }

This is the listBox1 in a new Form where i select the items. So i want to make that if i selected by clicking on it or moving over it on the same item it will not add it to the Lightnings List not the same one and not any other items !! Only if i moved over or clicked on an item wich is not in Lightnings List then add it !

您可以使用Enumerable.Any

var result = pdf1.Lightnings.Any(c=>lightningsRegions.Contains(c));

When using Contains, Equality is used to determine if the list indeed contains the instance you are checking for, but Equality doesn't have the same behavior if you are comparing reference types rather than value types.

So instead of using Contains, try using the Linq extension method Any() and in the delegate method given as parameter put the fields that are relevant to you.


To verify that what i am saying is indeed correct you can check when debugging and you will see that even though the list already contains what you are searching for your code will still add a new instance which for you is a duplicate but as instances go they are not equal unless they point to the same region in memory.


So, if for you it's relevant that lightningRegion.Text for instance ("Length 32 [41 - 73]") is not added twice then in the If clause check:

if (!pdf1.Lightnings.Any(lightningRegion => lightningRegion.Text == lightningRegions[i].Text)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM