views:

349

answers:

2

I have the following code:

public IQueryable<ITax> FindAllTaxes()
{
      return db.Taxes;
}

I am getting the following error

Cannot implicitly convert type 'System.Data.Linq.Table<Models.Tax>' to 'System.Linq.IQueryable<Interfaces.ITax>'

I am trying to use Interface where ever I go, but not sure how to convert this, any help?

A: 

try this

     public IQueryable<Taxe> FindAllTaxes() { return db.Taxes; }

or

     public IQueryable<Tax> FindAllTaxes() { return db.Taxes; }
Fredou
+3  A: 

We don't have covariance of generic types yet, I'm afraid. You can do IQueryable<Tax>, but not IQueryable<ITax>. You could introduce a conversion, but it'll probably break composability, rendering it useless. You could try it though:

return db.Taxes.Cast<ITax>();

In C# 4.0, this would probably work without the extra cast (although I haven't tried it).

Marc Gravell
Amazing how a little formatting can change the sense of a question. I was scratching my head over why IQueryable (non-generic) wouldn't work and lo and behold, it was really a generic of the interface he was after.
tvanfosson
I had the same - I started writing an answer about how Table<T> : IQueryable<T> : IQueryable, then thought "I wonder..." ;-p
Marc Gravell