Amazingly, I have not found a simple solution for the following behavior.
I have a List and I just want to check (with Linq) if there are multiple entries in it. That mean, i want to get a bool.
For Example:
List<string> listWithMultipleEntries = new List<string>()
{
"Hello",
"World",
"!",
"Hello"
};
This is maybe a solution I've ended with (I have not tested extensively but it seems to work)
if (listToCheck.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y).Any())
{
// Do something ...
}
but I would be surprised if there would not be any simpler solution (I really have not found one)
The group by option is probably the best but you could also do
if (listToCheck.Distinct().Count() != listToCheck.Count())
{
// Do sth.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With