How do I convert an array of T (IEnumerable<T>) to IQueryable<T> for processing with LINQ?
Asked Answered
G

2

5

I'm working on an app using a WCF server (using the new beta Mindscape LightSpeed ORM) and consuming the service at an ASP.NET MVC2 client. My entities show up at the client as array of T, or IEnumerable<T>. I want to do cool things with the data after it has arrived, but the LINQ syntax required IQueryable<T>.

I know there is a way to convert from IEnumerable<T> to IQueryable<T>, but I have had no luck so far searching for it. Can anybody help?

Thanks, Dave

Glazier answered 3/3, 2011 at 15:56 Comment(1)
Thanks for fixing my typo, Ani...Glazier
I
8

Well, I suspect you're thinking of Queryable.AsQueryable() - but what makes you think that you need to use IQueryable<T> to use LINQ? Converting an IEnumerable<T> to IQueryable<T> won't give you most of the benefits of IQueryable<T>.

LINQ to Objects is all based around IEnumerable<T>. If you're happy working with the data you've already received, that's all you need.

If, however, you want to express a query at your client and tell the WCF server to execute that query (e.g. performing the filtering in the database or at least at the WCF side), that's when you'd use IQueryable<T>. You might find my Edulinq post about IQueryable<T> useful. You'd need some way of representing the service as an IQueryable<T> though - and at that point you're beyond my WCF knowledge.

Invariable answered 3/3, 2011 at 15:57 Comment(2)
Thanks Jon! That's what I was looking for. I am implementing the PaginatedList helper class from the MVC NerdDinner example in my application, and it uses IQueryable<T> as the 'source' argument. I hadn't thought about modifying the example code to use IEnumerable<T> in place of IQueryable<T>, but that might work just as well.Glazier
@DaveN59: I suspect it will, yes.Invariable
B
0

Try the following method: AsQueryable()

Bunnie answered 3/3, 2011 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.