Reputation: 420
Here is code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication8
{
public interface MyBaseClass { }
public interface MyClass1 : MyBaseClass { }
public interface MyClass2 : MyBaseClass { }
public interface Filter { int Id { get; set; } }
public interface Filter1 : Filter { }
public interface Filter2 : Filter { }
public static class DbExt
{
public static IQueryable<T> WhereHistory<T>(this IQueryable<T> source, Func<Filter1, bool> expr) where T : MyClass1
{
Console.WriteLine("WhereEntityHistory");
return source;
}
}
public static class DocExt
{
public static IQueryable<T> WhereHistory<T>(this IQueryable<T> source, Func<Filter2, bool> expr) where T : MyClass2
{
Console.WriteLine("WhereEntityHistory");
return source;
}
}
class Program
{
static void Main(string[] args)
{
IQueryable<MyClass1> query1 = (new List<MyClass1>()).AsQueryable();
query1 = query1.WhereHistory(h => h.Id == 0);
IQueryable<MyClass2> query2 = (new List<MyClass2>()).AsQueryable();
query2 = query2.WhereHistory(h => h.Id == 7);
}
}
}
This code is compiled with error:
The call is ambiguous between the following methods or properties: 'ConsoleApplication8.DbExt.WhereHistory(System.Linq.IQueryable, System.Func)' and 'ConsoleApplication8.DocExt.WhereHistory(System.Linq.IQueryable, System.Func)'
Call is not ambiguous because of where conditions. How can I fix it?
Upvotes: 0
Views: 116
Reputation: 2078
if you want to filter by where T : MyClass1
why not just remove the generic and use
public static IQueryable<MyClass1> WhereHistory(this IQueryable<MyClass1> source, Func<Filter1, bool> expr)
Upvotes: -1
Reputation: 152626
Generic constraints are not part of a function's signature, so in order to distinguish between them you'd need to call them as static methods (or give them different names).
The method type inference algorithm considers only whether the method type arguments can be consistently inferred from the types of the arguments.
Source: Eric Lippert
Upvotes: 5