LINQ SingleOrDefault,Brush,我做错了什么?
LINQ SingleOrDefault, Brush, what I am doing wrong?
我通过 USB 通信将一些数据输入我的程序 -> RGB 3 字节。
我将这些字节放入 "new SolidColorBrush",然后得到 "Brush"。
Brush ColorActiveDark = new SolidColorBrush(Color.FromRgb(USBred, USBGreen, USBBlue));
后来我在"MyList"中搜索这个笔刷,但总是取默认值。为什么它在我的列表中找不到相同的值?
_illumination.SelectedColorAN = _illumination.ColorList.SingleOrDefault(x => x.Color == ColorActiveDark) ?? _illumination.ColorList[0];
MyList 的定义如下:
public class ColorItem
{
public string ColorName { get; set; }
public Brush Color { get; set; }
}
以及后来的 ColorList
public IList<ColorItem> ColorList { get; set; }
ColorList = new List<ColorItem>()
{
new ColorItem() { ColorName = "AppleGreen", Color = new SolidColorBrush(Color.FromRgb(76, 196, 23)) }, //
new ColorItem() { ColorName = "Mustard", Color = new SolidColorBrush(Color.FromRgb(255, 219, 88)) }, //
new ColorItem() { ColorName = "Red", Color = new SolidColorBrush(Color.FromRgb(255, 0, 0)) }, //
new ColorItem() { ColorName = "Device1", Color = new SolidColorBrush(Color.FromRgb(0, 31, 0)) }, //
new ColorItem() { ColorName = "Device2", Color = new SolidColorBrush(Color.FromRgb(0, 255, 0)) }, //
new ColorItem() { ColorName = "Device3", Color = new SolidColorBrush(Color.FromRgb(31, 0, 0)) }, //
};
我的错误在哪里?如有任何问题请提问。我希望我写得尽可能简单易懂。谢谢!
这是因为您正在比较对象的两个不同实例,它们不匹配。
相反,您可以搜索 ColorName
和 return:
_illumination.SelectedColorAN = _illumination.ColorList.SingleOrDefault(x => x.ColorName == "AppleGreen") ?? _illumination.ColorList[0];
或者,将 RGB 值存储在 ColorItem
对象中以匹配它。
public class ColorItem
{
public string ColorName { get; set; }
public Brush Color { get; set; }
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
}
这里的问题是当您比较 class 的不同实例时,条件 x.Color == ColorActiveDark
总是会失败。因此 ==
将检查引用是否相等(显然它们不相等)。相反,您想要的是通过实施 object.Equals
:
检查 class 的某些属性是否相等
class Brush {
override bool Equals(object other) {
Brush b = other as Brush;
if (b == null) return false;
return b.ColorName == this.ColorName;
}
}
或者通过比较 ColorItem
-属性 来更好地进行比较,其中您还必须为 ColorItem
实施 Equals
:
public class ColorItem
{
override bool Equals(object other) {
ColorItem c = other as ColorItem;
return c.Red == this.Red && c.Blue = this.Blue && c.Green == this.Green;
}
}
编辑:您还应该为 ==
添加一个 operator-implementation,如下所示为两个 classes:
public static bool operator == (Brush a, Brush b) {return a.Equals(b); }
相应地 ColorItem
。
据我了解,您总是从 _illumination.ColorList[0]
中获得价值。如果是这样,那是因为 SingleOrDefault
方法无法找到 你的集合中唯一一个符合你的条件的元素 并且它 returns null
(如果使用Single
方法,如果找不到单个元素,它会抛出错误)。所以你收到了 null
,最后你会得到 _illumination.ColorList[0]
的价值。
接下来是您拥有 null
的原因。您正在尝试通过引用的相等性而不是字段的值来与对象进行比较。如果您有时需要比较这些对象,也许最好为此重载 ==
运算符。另一种可能的情况是通过一个(或一些)有价值的字段(属性)来比较两个对象。另一种情况是创建您可以用作搜索条件的特定 Expression
我通过 USB 通信将一些数据输入我的程序 -> RGB 3 字节。
我将这些字节放入 "new SolidColorBrush",然后得到 "Brush"。
Brush ColorActiveDark = new SolidColorBrush(Color.FromRgb(USBred, USBGreen, USBBlue));
后来我在"MyList"中搜索这个笔刷,但总是取默认值。为什么它在我的列表中找不到相同的值?
_illumination.SelectedColorAN = _illumination.ColorList.SingleOrDefault(x => x.Color == ColorActiveDark) ?? _illumination.ColorList[0];
MyList 的定义如下:
public class ColorItem
{
public string ColorName { get; set; }
public Brush Color { get; set; }
}
以及后来的 ColorList
public IList<ColorItem> ColorList { get; set; }
ColorList = new List<ColorItem>()
{
new ColorItem() { ColorName = "AppleGreen", Color = new SolidColorBrush(Color.FromRgb(76, 196, 23)) }, //
new ColorItem() { ColorName = "Mustard", Color = new SolidColorBrush(Color.FromRgb(255, 219, 88)) }, //
new ColorItem() { ColorName = "Red", Color = new SolidColorBrush(Color.FromRgb(255, 0, 0)) }, //
new ColorItem() { ColorName = "Device1", Color = new SolidColorBrush(Color.FromRgb(0, 31, 0)) }, //
new ColorItem() { ColorName = "Device2", Color = new SolidColorBrush(Color.FromRgb(0, 255, 0)) }, //
new ColorItem() { ColorName = "Device3", Color = new SolidColorBrush(Color.FromRgb(31, 0, 0)) }, //
};
我的错误在哪里?如有任何问题请提问。我希望我写得尽可能简单易懂。谢谢!
这是因为您正在比较对象的两个不同实例,它们不匹配。
相反,您可以搜索 ColorName
和 return:
_illumination.SelectedColorAN = _illumination.ColorList.SingleOrDefault(x => x.ColorName == "AppleGreen") ?? _illumination.ColorList[0];
或者,将 RGB 值存储在 ColorItem
对象中以匹配它。
public class ColorItem
{
public string ColorName { get; set; }
public Brush Color { get; set; }
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
}
这里的问题是当您比较 class 的不同实例时,条件 x.Color == ColorActiveDark
总是会失败。因此 ==
将检查引用是否相等(显然它们不相等)。相反,您想要的是通过实施 object.Equals
:
class Brush {
override bool Equals(object other) {
Brush b = other as Brush;
if (b == null) return false;
return b.ColorName == this.ColorName;
}
}
或者通过比较 ColorItem
-属性 来更好地进行比较,其中您还必须为 ColorItem
实施 Equals
:
public class ColorItem
{
override bool Equals(object other) {
ColorItem c = other as ColorItem;
return c.Red == this.Red && c.Blue = this.Blue && c.Green == this.Green;
}
}
编辑:您还应该为 ==
添加一个 operator-implementation,如下所示为两个 classes:
public static bool operator == (Brush a, Brush b) {return a.Equals(b); }
相应地 ColorItem
。
据我了解,您总是从 _illumination.ColorList[0]
中获得价值。如果是这样,那是因为 SingleOrDefault
方法无法找到 你的集合中唯一一个符合你的条件的元素 并且它 returns null
(如果使用Single
方法,如果找不到单个元素,它会抛出错误)。所以你收到了 null
,最后你会得到 _illumination.ColorList[0]
的价值。
接下来是您拥有 null
的原因。您正在尝试通过引用的相等性而不是字段的值来与对象进行比较。如果您有时需要比较这些对象,也许最好为此重载 ==
运算符。另一种可能的情况是通过一个(或一些)有价值的字段(属性)来比较两个对象。另一种情况是创建您可以用作搜索条件的特定 Expression