Paritosh
Paritosh

Reputation: 4503

generic method, getting ambiguous invocation error

I'm using the Repository Pattern and was trying to build a generic method, as i have a few object which pull from their repositories in the same way:

My Repositories are created in this manner:

public interface IEntityRepository<T> : IDisposable

public interface IAuditorRepository : IEntityRepository<Auditor>

public class AuditorRepository : IAuditorRepository

The Auditor class if defined as:

public class BaseEntity : IObjectWithState

public partial class Auditor : BaseEntity

Aside from AuditorRepository, I have a few others such as BuildingRepository. Now I'm trying to create a generic method, in which I could pass the Repository:

    public List<EditSelectItemViewModel> GetItems<T>(IEntityRepository<T> repos) where T : BaseEntity 
    {
        var d = repos.All.Where(x => x.isActive).OrderBy(x => x.Name).Include(z => z.Contracts)
            .Select(y => new EditSelectItemViewModel
            {
                Id = y.Id,
                SourceName = y.Name,
                DisplayOnNew = y.DisplayOnNew,
                NumberOfTimesUsed = y.ActiveContracts.Count(a => a.isActive)
            }).ToList();

        return d;
    }

But I keep getting an error on the .Where stating that this is an ambiguous reference. All Repositories implement the All method, for example below is the AuditorRepository version:

    public IQueryable<DomainClasses.Auditor> All
    {
        get { return _context.Auditors; }
    }

The IEntityRepostiory is defined as:

    public interface IEntityRepository<T> : IDisposable
{
    IQueryable<T> All { get; }
    //List<T> AllRequests { get; }
    IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
    T Find(params object[] keyValues);
    void InsertOrUpdateGraph(T entityGraph);
    void InsertOrUpdate(T entity);
    void Delete(params object[] keyValues);

}

and the baseEntity is:

    public class BaseEntity : IObjectWithState
{
    public State State { get; set; }
}

State is defined as:

    public interface IObjectWithState
{
    State State { get; set; }
}
public enum State
{
    Added,
    Unchanged,
    Modified,
    Deleted
}

Upvotes: 1

Views: 675

Answers (1)

J&#228;mes
J&#228;mes

Reputation: 7265

As I initially thought, the Ambiguous invocation error occurs because the lambda expression x => x.isActive is not valid, due to a lack of definition of the variable isActive in the class BaseEntity (class used as constraint of the type parameter in the method GetItems).

The error can be reproduced using this code:

(new object[0]).AsQueryable().Where(o => o == notDefined);

In my context, notDefined is not defined, raising at the same time the Ambiguous invocation error on the Where extension method. As soon as the lambda expression is fixed (e.g. o != null), the error is no longer thrown.

A screenshot of this Ambiguous invocation error with the other methods that match.

enter image description here

Upvotes: 4

Related Questions