1

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
4
  • 1
    What do you mean by i can't do that like this exactly? Commented 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 } Commented Dec 23, 2016 at 11:00
  • 1
    Why not use a for loop and break once you find the duplicate if you expect one match at max Commented Dec 23, 2016 at 11:00
  • @S.Akbari Sorry i've updated my question Commented Dec 23, 2016 at 11:05

4 Answers 4

5

You can check by this way

if (duplicates.Any(a => a.sFolder == myObject.sFolder))
{
 // Do stuff
}
answered Dec 23, 2016 at 11:00
0
1

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
1

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
0

You can use foreach

 foreach (var item in duplicates)
 {
 if (item.sFolder == myObject.sFolder )
 {
 // Do stuff
 break;
 }
 }
answered Dec 23, 2016 at 11:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.