What instantiate-able types implementing IQueryable<T> are available in .Net 4.0?
Asked Answered
P

4

52

Within the context of C# on .Net 4.0, are there any built-in objects that implement IQueryable<T>?

Penneypenni answered 7/2, 2012 at 1:38 Comment(0)
A
73

IQueryable objects are produced by Queryable Providers (ex. LINQ to SQL, LINQ to Entities/Entity Framework, etc). Virtually nothing you can instantiate with new in the basic .NET Framework implements IQueryable.

IQueryable is an interface designed to be used to create Queryable providers, which allow the LINQ library to be leveraged against an external data store by building a parse-able expression tree. By nature, Queryables require a context - information regarding what exactly you're querying. Using new to create any IQueryable type, regardless of whether it's possible, doesn't get you very far.

That being said, any IEnumerable can be converted into an IQueryable by using the AsQueryable() extension method. This creates a superficially-similar, but functionally very different construct behind the scenes as when using LINQ methods against a plain IEnumerable object. This is probably the most plentiful source of queryables you have access to without setting up an actual IQueryable provider. This changeover is very useful for unit-testing LINQ-based algorithms as you don't need the actual data store, just a list of in-memory data that can imitate it.

Airwoman answered 7/2, 2012 at 1:53 Comment(5)
Thanks. Your scenario described (unit testing) is pretty much what I'm doing. Do you know of any good documentation on the construct differences you mention? I'm curious to learn more about how different the object is when using the AsQueryable() extension method.Penneypenni
It's VERY different. Instead of a chain of Iterator objects as produced by IEnumerable Linq methods, IQueryable methods add nodes to an Expression tree. This tree is not, by itself, executable as code like an Iterator. But, it is serializable, parseable, etc so you can determine the structure of the query and turn it into a query in another language like SQL.Airwoman
Look at this question and all related links: #253285Airwoman
I think you helped me learn two things here - #9010884. When using "linq to objects" I am working with a chain of iterators, whereas with L2Sql or L2E is an expression tree eventually converted to SQL or some other query language the queryable provider knows? Am I understanding decent?Penneypenni
Yes, you have the basic concept. Keep reading; there are important differences that shake out of both, but the basic gist is that while both allow "lazy" evaluation of whatever construct you've built, IEnumerable Linq is for working with vanilla in-memory object collections, while IQueryable Linq is for talking to "out-of-memory" data sources.Airwoman
I
3

Well, your question is kinda weird... but I believe that if you look at an interface in Reflector, it will give you a list of implementers in the loaded assemblies.

As a disclaimer I have not used Reflector since it went pay-for-play so I might be wrong.

Intromission answered 7/2, 2012 at 1:48 Comment(0)
H
3

EntityCollection does, as does EnumerableQuery.

Not that I think either of these is going to get you anywhere. To help, we need to know what you are really trying to solve. If you are writing a LINQ provider, you should read this: http://msdn.microsoft.com/en-us/library/bb546158.aspx.

They recommend writing your own implementation.

Hauler answered 7/2, 2012 at 1:51 Comment(2)
Thank you. Sorry for the confusion in the initial post; I'm not looking for help with my code, just learning new C# objects to play with.Penneypenni
@AndreyTch, EntityCollection doesn't.. DbQuery<T>, DbSet<T> etc do but I'm unsure what's publicly instantiable.Peculiarity
K
1

If you are looking for a way to instantiate an empty list of IQueryable, then you can use this:

IQueryable<MyEntity> = Enumerable.Empty<MyEntity>().AsQueryable()
Kauffman answered 31/10, 2022 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.