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 Query
- 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
More Query from same tag
- list in list in a single linq query
- Making an outer join on two anonymous type lists using LINQ in Entity Framework
- Linq to split/analyse substrings
- Multiple Linq functions as parameter
- Sort table with million rows , LinQ connection
- Count the hits in a chained chart
- LINQ in ItemBinding to add columns to DataGrid
- Linq Comment VB
- Hydrate property via joining another List<Property> on unique Id
- Using Linq in VB.NET to verify result set is NULL
- Help with writing a linq query
- How can I create a dynamic Select on an IEnumerable<T> at runtime?
- LINQ: How to skip one then take the rest of a sequence
- How can I combine IObservable<T>.Throttle() with some other event source using Reactive Extensions?
- How do I get Ionide to see .net core Linq and AspNetCore namespaces?
- When to use BlockingCollection and when ConcurrentBag instead of List<T>?
- Use Linq.Any() inside a Linq.Where() on CosmosDb
- Includes child and child's objects when the child object is not list or collection
- How to order by C# list so that the duplicate items appear in the reverse order of their insertion
- How to query a list<T> using LINQ
- Update value of a property using LINQ
- IEqualityComparer Exception with Linq (NotSupportedException)
- Get the row with the max(DateTime) LINQ
- Linq - Query compare to record before
- I am trying to learn how to bind an IEnumerable LINQ collection to a repeater
- Linq filtering by class array properties
- Can't enumerate LinQ results with left join
- Entity Framework Compiled Query
- Select grouped result
- Dapper nested object query - not populating all properties