[英]How to check if a list of object contains an object with a specified properties
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
语句来检查列表是否包含具有特定属性的对象:
if (duplicates.Contains(myObject.sFolder)) // "myObject.sFolder" raise an error (of course)
{
// Do stuff
}
Is there a simple way to do it? 有简单的方法吗?
You can check by this way 你可以这样检查
if (duplicates.Any(a => a.sFolder == myObject.sFolder))
{
// Do stuff
}
Not sure on whats being compared here but something like this? 不知道在这里比较什么,但是像这样?
if (duplicates.Any(x => x.sFolder == myObject.sFolder))
{
// Do stuff
}
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
}
You can use foreach
您可以使用
foreach
foreach (var item in duplicates)
{
if (item.sFolder == myObject.sFolder )
{
// Do stuff
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.