I've added services into my MVC/EF project to act as a layer between my controller and repositories.
I'm having trouble copying a method from the repo to the service. I'm trying to use Count() in the service but keep receiving error does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type .. could be found
I've implemented it exactly the same way as the repository has so I don't know why it's failing
Repositories:
public abstract class Repository<CEntity, TEntity> : IRepository<TEntity> where TEntity : class
where CEntity : DbContext, new()
{
private CEntity entities = new CEntity();
protected CEntity context
{
get { return entities; }
set { entities = value; }
}
public virtual int Count
{
get { return entities.Set<TEntity>().Count(); }
}
public virtual IQueryable<TEntity> All()
{
return entities.Set<TEntity>().AsQueryable();
}
}
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
int Count { get; }
IQueryable<TEntity> All();
}
Service:
public class CampaignService : ICampaignService
{
private readonly IRepository<Campaign> _campaignRepository;
public CampaignService(IRepository<Campaign> campaignRepository)
{
_campaignRepository = campaignRepository;
}
public int Count
{
get { return _campaignRepository.Count()**; }
}
public IQueryable GetAll()
{
return _campaignRepository.All();
}
}
public interface ICampaignService
{
int Count{ get; }
IQueryable GetAll();
}
** it fails on this line.
`Error 4 'MarketingSystem.Repositories.Common.IRepository' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'MarketingSystem.Repositories.Common.IRepository' could be found (are you missing a using directive or an assembly reference?)
The GetAll()/All() methods work fine, but Count() doesn't.
Can anyone spot and explain where I'm going wrong?
You are trying to call Count() on repository. Your repository is neither IQueryable<T> nor IEnumerable<T>, so extension method Count() is not available to you. Actually your repository implements only IRepository<TEntity> Which has only three members available - Dispose(), Count and All(). I think you need to call Count property from this interface.
get { return _campaignRepository.Count; }
NOTE - Count is not a method - its a property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With