Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用LINQ检查列表中是否存在位图源<;位图源>;_C#_Linq - Fatal编程技术网

C# 尝试使用LINQ检查列表中是否存在位图源<;位图源>;

C# 尝试使用LINQ检查列表中是否存在位图源<;位图源>;,c#,linq,C#,Linq,我需要检查列表中是否已经存在位图源,但我不确定应该比较什么。将检查项目并将其添加到列表中的方法从WPFUserControlrouted命令中获取BitmapSource作为参数 我想这样做: if(!selectedImages.Any(x => x.SomeBitmapSourceProperty == e.Parameter.SomeBitmapSourceProperty) selectedImages.Add(e.Parameter as BitmapSource)

我需要检查
列表
中是否已经存在
位图源
,但我不确定应该比较什么。将检查项目并将其添加到列表中的方法从WPF
UserControl
routed命令中获取
BitmapSource
作为参数

我想这样做:

if(!selectedImages.Any(x => x.SomeBitmapSourceProperty == e.Parameter.SomeBitmapSourceProperty)
     selectedImages.Add(e.Parameter as BitmapSource)
我将使用什么来比较两个
位图源
,并且我实际上能够从
e.Parameter
访问该属性?

尝试:

 if (selectedImages.Count(x =>
         x.SomeBitmapSourceProperty == e.Parameter.SomeBitmapSourceProperty
     ) == 0) selectedImages.Add(e.Parameter as BitmapSource);

问题并不完全清楚,但您似乎希望使用引用相等(即两个
BitmapSource
值指向同一对象)进行检查。您可以通过以下方式完成此操作:


您可以比较此对象的哈希代码。(方法
GetHashCode()
var candidate = (BitmapSource)e.Parameter;
if(!selectedImages.Contains(candidate))
{
    selectedImages.Add(candidate);
}