tags:

views:

629

answers:

7

Hi all,

I have recently written an application(vb.net) that stores and allows searching for old council plans. Now while the application works well, the other day I was having a look at the routine that I use to generate the SQL string to pass the database and frankly, it was bad.
I was just posting a question here to see if anyone else has a better way of doing this.

What I have is a form with a bunch of controls ranging from text boxes to radio buttons, each of these controls are like database filters and when the user hits search button, a SQL string(I would really like it to be a LINQ query because I have changed to LINQ to SQL) gets generated from the completed controls and run.

The problem that I am having is matching each one of these controls to a field in the database and generating a LINQ query efficiently without doing a bunch of "if ...then...else." statements. In the past I have just used the tag property on the control to link to control to a field name in the database.

I'm sorry if this is a bit confusing, its a bit hard to describe. Just throwing it out there to see if anyone has any ideas.

Thanks Nathan

A: 

You could maybe wrap each control in a usercontrol that can take in IQueryable and tack on to the query if it is warranted.

So your page code might go something like

var qry = from t in _db.TableName
      select t;

then pass qry to a method on each user control

IQueryable<t> addToQueryIfNeeded(IQueryable<t> qry)
{
   if(should be added)
      return from t in qry
           where this == that
           select t;
   else
      return qry
}

then after you go through each control your query would be complete and then you can .ToList() it. A cool thing about LINQ is nothing happens until you .ToList() or .First() it.

Kevin Sheffield
I don't mind this method but I was hoping to avoid writing a heap of custom controls just for this.
Nathan W
A: 

When programming complex ad-hoc query type things, attributes can be your best friend. Take a more declarative approach and decorate your classes, interfaces, and/or properties with some custom attributes, then write some generic "glue" code that binds your UI to your model. This will allow your model and presentation to be flexible, without having to change 1000s of lines of controller logic. In fact, this is precisely how Microsoft build the Visual Studio "Properties" page. You may even be able use Microsoft's "EnvDTE.dll" in your product depending on the requirements.

slf
Would you be able to show me an example of some generic "glue" code.
Nathan W
http://www.castleproject.org/ActiveRecord/
slf
A: 

I don't know about the performance here, but if you set up the LINQ to SQL data context class you should be able to query a database table with a .Select(...) or .Where(...). You should be able to build lambda expressions for either of these dynamically. You might look into dynamic generation of lambda expressions for this purposes. I have done everything up to the point of the dynamic lambda generation, but it is possible.

Jason Jackson
A: 

I'm not 100% sure how to achieve this but I know where a good place to start would be, in the ASP.NET MVC source. In recent versions it is capable of taking the form response and pass it into a helper method which does the writing to a LINQ data source.

I believe MVC is C# so if you're looking for a VB translation you could try using .NET Reflector and converting it back to VB.

Slace
A: 

I think you are searching how to create a "Dynamic" Linq Query, Here is an example about how to do it with a library of extension methods. Those methods take string arguments instead of type-safe language operators.

CMS
A: 

I don't mind sfusco's method by using attributes. The only thing that i'm not sure of is where to attach the attributes to because If I attach then to the controls declaration which is in the designer code it will get regenerated when the form changes.

Or am I completely misunderstanding sfusco's methods?

Nathan W
A: 

I think perhaps the right way to do this would be an extender provider: MSDN documentation

Then, you can use the editor to provide the field names to hook up with, and your extender provider can be passed an IQueryable<T>, add the criteria, and return an IQueryable<T>.

Jonathan