使用LINQ,我可以验证所有对象的属性是否具有相同的值吗?
- 论坛
- 使用LINQ,我可以验证所有对象的属性是否具有相同的值吗?
7 浏览
使用LINQ,我可以验证所有对象的属性是否具有相同的值吗?
我有一个Crate对象,其中包含一个KeyValuePairs的列表。目前,我正在遍历每个键值对,看看列表中的所有项的kvp.Value.PixelsWide是否相同。如果相同,则返回true,否则返回false。
我目前的方法如下所示:
public bool Validate(Crate crate)
{
int firstSectionWidth = 0;
foreach (KeyValuePair
{
if (firstSectionWidth == 0)//第一次循环
{
firstSectionWidth = kvp.Value.PixelsWide;
}
else //不是第一次循环
{
if (kvp.Value.PixelsWide != firstSectionWidth)
{
return false;
}
}
}
return true;
}
我想知道是否可以在LINQ查询中执行此操作?
提前感谢任何帮助!