Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is calling AsQueryable<T> on a DbSet<T> "safe"?

Say I want to unit test Entity Framework's query logic, one way would be to convert the DbSet<T> to IQueryable<T> before building the expression trees for easy mocking. Is this "safe" and is there anything to be aware about?

like image 374
Dante Avatar asked Jan 04 '13 13:01

Dante


People also ask

What is the difference between AsEnumerable and AsQueryable?

AsEnumerable preserves deferred execution and does not build an often useless intermediate list. On the other hand, when forced execution of a LINQ query is desired, ToList can be a way to do that. AsQueryable can be used to make an enumerable collection accept expressions in LINQ statements.

Can IQueryable be async?

ToListAsync(IQueryable)Creates a List<T> from an IQueryable by enumerating it asynchronously.


1 Answers

It is not only safe, it is legal and fully standard. This is what OO is all about. You just downcast. A DbSet HAS to be a IQUeryable, per the contract defined by the designers.

like image 193
TomTom Avatar answered Sep 18 '22 05:09

TomTom