tags:

views:

119

answers:

1

I have several queries identical except for the subject and parameters of the query and I am trying to formulate a 'generalised' query to avoid multiple lines of almost identical code.

I have the following code:

Code Snippet

 private IEnumerable doQuery(string rangeVar,
                             DataSet dsSubTable,
                             string qrySubject,
                             string subTableKey,
                             string subTableDescription,
                             string fldName)
    {
        qryStart = dtmpickFrom.Value;
        qryEnd = dtmpickTo.Value;

        var groupQuery =
            from trans in dataSet.Transaction    
            where ((trans.T_Date >= qryStart) && (trans.T_Date <= qryEnd))
            from rangeVar in dataSet.dsSubTable  <<<<<<
                 where trans.qrySubject == rangeVar.subTableKey

            select new    ..... <snipped>

The compiler won't accept the dsSubTable (shown <<<<<) with the following message:

'ExpenditureLINQDataSets.Expenditure' does not contain a definition for 'dsSubTable' and no extension method 'dsSubTable' accepting a first argument of type 'ExpenditureLINQDataSets.Expenditure' could be found (are you missing a using directive or an assembly reference?)'

I realise this, of course (if I try to give dsSubTable a type of Expenditure..... only the actual table names defined in my DataSet appear in Intellisense.)

Is it possible to generalise such LINQ queries or must I live with multiple, allmost identical queries in my code?

Any assistance appreciated!

+1  A: 

If you pass in dsSubTable as a DataTable then you can use it directly in a from blah in dsSubTable.

But it feels like your abusing Linq. The power of Linq is that it is not Sql, so why are you writing a method that instead of string concatenating Sql together, you're building up a Linq expression.

Linq allows you to think of your data in terms of Sets and that's what I find really powerful about it. I encourage you to think about what you are trying to accomplish when you call 'doQuery' and encode those queries as first class operations.

ecoffey
I don't think I'm abusing LINQ. I'm using it extensively in my application. I just wanted to know if I could avoid scattering t a lot of very similar queries through my code.
Oh it has nothing to do with how much you're using linq. I'm just saying that Linq allows you to take a conceptual step up, what you're trying to do doesn't reflect that. I know I sound like an ass, but there are probably more Model and Architecture concerns implied by your question then anything
ecoffey
Also, damn character limits on a comment field :-P
ecoffey
My program is a straightforward WinForms app used by myself on my PC. I don't see any particular Model/Architecture issues, just a desire for neat code and elimination of numerous 'almost identical' queries.
http://tomasp.net/articles/dynamic-linq-queries.aspx I don't know if this will help, but you might find it cool :-)
ecoffey
Thanks, I'll have a look and see how I might use the techniques described.