7

I have an entity object 'User' which implements 'IUser':

IQueryable<User> users = Db.User;
return users;

But what I actually want to return is:

IQueryable<IUser>

So what is the best way to convert

IQueryable<User>

to

IQueryable<IUser>

without actually running the query? Right now I am doing this but it seems like a hack:

IQueryable<IUser> users = Db.User.Select<User, IUser>(u => u);
Mike Comstock
  • 6,640
  • 10
  • 36
  • 41
  • 1
    Craig Stuntz is correct: L2E doesn't support the Cast method. If fails with: "Unable to cast the type 'MyType' to type 'MySubTypet'. LINQ to Entities only supports casting Entity Data Model primitive types.". I deleted my answer since it didn't provide any value. – bruno conde Aug 06 '09 at 17:17
  • Doesn't this eager load the results? it doesn't get the whole table when for each part of the query? – Maslow Feb 23 '12 at 22:37
  • Which .NET version? In .NET 4 you should be able to assign any IQueryable to a variable of type IQueryable without the need for a cast of all the entries is the list... – jessehouwing May 04 '12 at 20:03

1 Answers1

5

Your "hacky" solution looks fine to me.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273