Question

I have tried some ways to use LINQ dynamic queries - LINQKit and LINQ Dynamic Query Library. I do not like the second because it some way kills the LINQ idea - to be able to check queries at compile time. And with LINQKit I did not find a good example for my scenario. Also I do not like excessive using of reflection.

My scenario is the following. I have a web service which is doing business logic and DAL logic. The webforms application is separated. I have some page with tickboxes for each field the user would like to filter, and also a text box to enter each filter value. My web service has a method GetByFilter where I pass some List. QueryObject is a class with string: filedName, object: fieldValue.

Then my webservice receives list of query objects and now comes the big question: how to translate it to LINQ query if the field count and filter values may vary?

What's even worse - I do not use LINQ2SQL but I use some custom DAL with repositories which may return IQuery if needed (like this one: http://msdn.microsoft.com/en-us/magazine/dd569757.aspx scroll to Repository).

I know I can use paging with LINQ2SQL: var PagedData = query.Skip((iPageNum - 1) * iPageSize).Take(iPageSize);

So how then I can get the dynamic query parameters (and iPageNum and iPageSize) from LINQ to my underlying DAL implementation to execute those queries in a dataprovider specific way? Maybe I have to implement my DAL as some LINQ data provider (I have no idea how to do it)?

The problem is - I do not want to depend on LINQ2SQL (then I could just implement my Repositories as a wrappers for LINQ2SQL) but at the same time I want to have LINQ querying abilities everywhere outside my DAL. Is it possible?

Was it helpful?

Solution

It's certainly possible, what you're looking to is create your own IQueryable, a "custom LINQ provider"

Here's the MSDN article: http://msdn.microsoft.com/en-us/library/bb546158.aspx and Matt Warren's got a blog series on it: http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx

I recommend starting on it to see how hard it might be and whether it's worth it for you!

If you decide against it, have a read of this blog post: http://www.hanselman.com/blog/RealWorldAppsInDaysNotWeeks.aspx and see if ADO.Net Data Services (Astoria) are of any use to you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top