score:2
Accepted answer
That's because DerivedEnumerator
implements both IEnumerable<BaseThingy>
and IEnumerable<DerivedThingy>
(indirectly via IReadOnlyColleciton
). So when you do
var y = derivedEnumerator.Where(s => s.GetHashCode() == s.GetHashCode());
Compiler cannot figure out what is the type of s
in lamdba. Is that DerivedThingy
? Is that BaseThingy
? Both are possible. So you need to tell it explicitly what that is:
var y = derivedEnumerator.Where<DerivedThingy>(s => s.GetHashCode() == s.GetHashCode());
or
var y = derivedEnumerator.Where((DerivedThingy s) => s.GetHashCode() == s.GetHashCode());
That said, it's quite unusual design and you might reconsider it. How exactly depends on what you are trying to do (so, why do you need to inherit enumerators this way).
score:0
Ambiguity issue
namespace ClassLibrary1
{
public class BaseThingy { }
public class DerivedThingy : BaseThingy { }
public class BaseEnumerator : IReadOnlyCollection<BaseThingy>
{
public int Count => throw new NotImplementedException();
public IEnumerator<BaseThingy> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class DerivedEnumerator : BaseEnumerator, IReadOnlyCollection<DerivedThingy>
{
IEnumerator<DerivedThingy> IEnumerable<DerivedThingy>.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class Application
{
public void Main()
{
BaseEnumerator baseEnumerator = new BaseEnumerator();
DerivedEnumerator derivedEnumerator = new DerivedEnumerator();
// Works...
var x = baseEnumerator.Where(s => s.GetHashCode() == s.GetHashCode());
// Works now
var y = derivedEnumerator.Where<DerivedThingy>(s => s.GetHashCode() == s.GetHashCode());
}
}
}
Source: stackoverflow.com
Related Articles
- C#: Using LINQ on a derived iterator class when both the derived class and its base implement IEnumerable
- C# LINQ query fails when using IsAbstract property during derived class scan with reflection?
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Class has invalid expression term "from" when querying using LINQ
- Casting derived class to base class using LINQ to Entities
- Linq sub query when using a repository pattern with EF code first
- Failure to implicitly convert class to base class when using LINQ IEnumerable.ToDictionary()
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Using only Linq or Lambda, how do I combine both pieces of code to return List<Entity> e?
- Using a class derived from generic list with LINQ
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- Linq query throws entity or complex type cannot be constructed in a linq to entity, even when i remove the class name just using select new { ..}
- Convert string[] to int[] in one line of code using LINQ
- Entity-framework code is slow when using Include() many times
- Ambiguous call when using LINQ extension method on DbSet<T>
- How to handle nulls in LINQ when using Min or Max?
- How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?
- Why does this Linq Cast Fail when using ToList?
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- What actually happens when using async/await inside a LINQ statement?
- Differences between IEquatable<T>, IEqualityComparer<T>, and overriding .Equals() when using LINQ on a custom object collection?
- Using a partial class property inside LINQ statement
- Performance concern when using LINQ "everywhere"?
- Why do I have to copy "this" when using LINQ in a struct (and is it OK if I do)?
- linq deferred execution when using locks in methods that return IEnumerable
- How does LINQ defer execution when in a using statement
- Exception when using LINQ SUM
- Using Linq to Select properties of class to return IEnumerable<T>
- .FirstOrDefault() is returning null on a non-empty IQueryable
- linq group by hour range?
- Can we do Parallel EF LINQ Queries?
- Anonymous type for datarow-object isn't working
- Convert Type1 To type2 Using LINQ with Subselect projection
- SQL query with subquery equivalent for Linq-to-SQL
- Linq groupby on values of List of dictionary<string,string> with unknown number of keys or key names
- Entity Framework Core 6 (preview) forces me to use .AsEnumerable()
- Threading and IEnumerable; "Collection Was Modified"-exception
- EF Core Grouping Multiple Joined Tables
- What is LINQ Actually Compiled To?
- How can I use LINQ with a class that has parallel arrays?
- Is there a not in sub-query for Linq to Enties as in this T-SQL
- Getting a Count from a linq query
- Linq groupby and global count (not count per group)
- Check property value in nested list with unknown levels
- How to refactor Linq Group By and Foreach statement into one Linq Query
- Splitting string into multiple strings using LINQ
- Why can't I read this XML values with XElement?
- Linq query order by giving me issues