I have a list of duplicates object:
var duplicates = workspace.Maps.GroupBy(m => m.sFolder).SelectMany(grp => grp.Skip(1)).ToList();
I want an if
statement to check if the list contains an object with a particular properties:
if (duplicates.Contains(myObject.sFolder)) // "myObject.sFolder" raise an error (of course)
{
// Do stuff
}
Is there a simple way to do it?
Mostafiz
7,3523 gold badges30 silver badges42 bronze badges
asked Dec 23, 2016 at 10:57
-
1What do you mean by i can't do that like this exactly?Salah Akbari– Salah Akbari12/23/2016 10:59:47Commented Dec 23, 2016 at 10:59
-
Not sure on whats being compared here but something like this? if (duplicates.Any(x => x.sFolder == myObject.sFolder)) // But i can't do that like this... { // Do stuff }Monolithcode– Monolithcode12/23/2016 11:00:20Commented Dec 23, 2016 at 11:00
-
1Why not use a for loop and break once you find the duplicate if you expect one match at maxMaher Fattouh– Maher Fattouh12/23/2016 11:00:22Commented Dec 23, 2016 at 11:00
-
@S.Akbari Sorry i've updated my questionThomas– Thomas12/23/2016 11:05:02Commented Dec 23, 2016 at 11:05
4 Answers 4
You can check by this way
if (duplicates.Any(a => a.sFolder == myObject.sFolder))
{
// Do stuff
}
answered Dec 23, 2016 at 11:00
Not sure on whats being compared here but something like this?
if (duplicates.Any(x => x.sFolder == myObject.sFolder))
{
// Do stuff
}
answered Dec 23, 2016 at 11:01
Just in case you need the duplicate object for further inspection try
var duplicate = duplicates.FirstOrDefault(m => m.sFolder == myObject.sFolder);
if(duplicate != null)
{
// Further check duplicate
}
answered Dec 23, 2016 at 11:04
You can use foreach
foreach (var item in duplicates)
{
if (item.sFolder == myObject.sFolder )
{
// Do stuff
break;
}
}
lang-cs