May 6, 2012 at 6:08 PM
Edited May 6, 2012 at 8:27 PM
Today I've started an experimental subproject for Helpful Libraries that would be an IQueryable content query.
Due to how IContentQuery works (it changes the type when using predicates, that means when one uses multiple record types to compose a query) it's even theoretically impossible to provide a full implementation. The aim is to implement IQueryable<ContentItem>
with some basic capabilities.
Admittedly I once want this to use this with Web API webservices.
You can get the source from the
ContentQueryable (pun intended) branch of Helpful Libraries. The current version is purely experimental, capable of translating the following LINQ expressions to their ContentQuery counterparts:
- Where: to filter on content types, only equality operator supported and only the innermost expression evaluated
To be done:
- Skip and Take implementation
- Fallback to LINQ to objects if something is not supported
- Smarter Where
- Where with ids would be possible with
The code was made after
this MSDN tutorial and I must say writing a LINQ provider is quite horrifying. Although it works, since in great amount of the time I had barely an idea what I was doing the source should have a decent amount of WTF/LOC ratio.
Any feedback is warmly welcome!
var count = new ContentQueryable(_contentManager.Query()).Count();
var pageCount = new ContentQueryable(_contentManager.Query()).Where(content => content.ContentType == "Page").Count();
var pagesAndBlogs = new ContentQueryable(_contentManager.Query()).Where(content => content.ContentType == "Page" || content.ContentType == "Blog").ToArray();