score:2
Accepted answer
In your line of code:
return _alarms.FirstOrDefault(a => a.InternalAlarm == alarm);
the a.InternalAlarm == alarm
is calling operator==
rather than object.Equals()
to do the comparison.
You can fix this by changing the code to:
return _alarms.FirstOrDefault(a => a.InternalAlarm.Equals(alarm));
The following program demonstrates the difference:
using System;
static class Program
{
public static void Main()
{
MyAlarm ma1 = new MyAlarm { Prop1 = 1, Prop2 = 2 };
MyAlarm ma2 = new MyAlarm { Prop1 = 1, Prop2 = 2 };
VisibleAlarm va1 = new VisibleAlarm { InternalAlarm = ma1, Message = "message" };
VisibleAlarm va2 = new VisibleAlarm { InternalAlarm = ma2, Message = "message" };
Console.WriteLine(ma1 == ma2); // True
Console.WriteLine(va1 == va2); // True
Console.WriteLine(va1.InternalAlarm == va2.InternalAlarm); // False
Console.WriteLine(va1.InternalAlarm.Equals(va2.InternalAlarm)); // True
}
public interface IAlarm
{
int Prop1 { get; }
int Prop2 { get; }
}
public record MyAlarm : IAlarm
{
public int Prop1 { get; init; }
public int Prop2 { get; init; }
}
public record VisibleAlarm
{
public IAlarm InternalAlarm { get; init; }
public string Message { get; set; }
}
}
Output:
True
True
False
True
The reason this isn't working for you is because InternalAlarm
is of type IAlarm
and not MyAlarm
. If you change the type to MyAlarm
, you'll get the comparison as true
rather than false
.
This is because IAlarm
does not define an operator==
(note: such an operator would be static).
Source: stackoverflow.com
Related Articles
- Comparing record types with LINQ in C#
- Can't add a new record with an integer value into database by using linq from code C#
- LINQ Select Distinct with Anonymous Types
- How to insert a record with LINQ and C# and return the Primary Key of that record
- How can anonymous types be created using LINQ with lambda syntax?
- VB.NET linq group by with anonymous types not working as expected
- How to group by custom types with linq
- Distinct in LINQ with anonymous types (in VB.NET)
- Exclude types form IEnumerable with linq
- Concatenating two lists of different types with LINQ
- Linq to SQL: Where clause comparing a Nullable<DateTime> with a SQL datetime null column
- Dapper parameterised queries with LINQ autogenerated types
- LINQ Source Code Available
- Combining collections of different types with LINQ
- LINQ Join On Multiple Columns With Different Data Types
- Difference of two List with different types using LINQ
- Comparing two collections with IEquatable while using only LINQ
- Linq with where clause in many-to-many EF Code First object
- Get latest record and group with highest date - LINQ
- LINQ for comparing two lists with complex entities
- LINQ select next record with each matching result
- Problems with nullable types in a LINQ Function
- check if record is last or first in list with linq
- Problem with adding record using LINQ to SQL
- C# .Net Linq adding new record with identity field
- Inconsistent return types of linq queries with orderby clauses
- Linq 2 Entities : Performing a join on two columns with different types
- Error when trying to Find a record in a simple table with Linq to Entities
- Selecting last record by linq fails with "method not recognised"
- Delete record using linQ with lambda Expression
- Will the EF Fluent API allow mapping an optional non-standard FK to a non-standard primary key or using non-standard projection
- When should I use LINQ for C#?
- f#: Only parameterless constructors and initializers are supported in LINQ to Entities
- Cannot use aggregate on a List<T>
- Cannot call methods on varbinary using nettopologysuite
- Passing a List of values and rerturning a Dictionary for their associated results
- LINQ Statement with where clause makes the execution slow
- Using XhtmlTextWriter with XmlTextReader
- convert SQL to LINQ or imporve my query please
- LINQ Source Code Available
- Send a variable in where clause LINQ PHP
- Can I capture a local variable into a LINQ Expression as a constant rather than a closure reference?
- Simple Eager / Lazy loading examples in linq2sql
- write linq similiar where in select from with inner join in sql query
- Error says x is not a member of y, but it is
- Linq to XML, extracting attributes and elements
- How can I Make a LINQ query expression dynamic
- How to use the let clause in C#
- LINQ - Getting all child records from all parents
- EmbeddedResourceVirtualPathProvider and @Html.TextBoxFor(Linq expression)