views:

294

answers:

5

Okay,

This may be really simple or it may not even be possible, or I am just having a brain freeze :)

Here is an example of what I am trying to do:

public void SomeMethod(bool include)
        {
            using (AccountDataContext db = AccountContextFactory.CreateContext())
            {
                if (include)
                {
                    var query = from a in db.FundingTypes where a.FundingTypeId == 1 select a;
                }
                else
                {
                    var query = from a in db.FundingTypes where a.FundingTypeId != 1 select a;
                }
            }
        }

I would like to dynamically change the != and = without having to write an entire new query. The query that I am using in real life is very large and I don't like code duplication.

Thought or Ideas?

Thanks

A: 

How about this:

public void SomeMethod(bool include, Func<int, bool> query)
{
    using (AccountDataContext db = AccountContextFactory.CreateContext())
    {
         var query = from a in db.FundingTypes where query(a.FundingTypeId) select a;
    }
}
Darin Dimitrov
A: 

Try this:

SomeMethod(bool include)
{
    using (AccountDataContext db = AccountContextFactory.CreateContext())
    {
        var query = from a in db.FundingTypes where !include ^ (a.FundingTypeId == 1) select a;
    }
}

Edit simplified the logic with an XOR

dustyburwell
Ooo...as rstevens mentioned, the XOR operator should simplify that to (a.FundingTypeId != 1) ^ include
dustyburwell
Hint: draw the truth table. There are only four possible combinations. Once you've drawn the truth table it should be quite obvious what the simplification is.
Eric Lippert
A: 

Try this:

var predicate = include
    ? (Func<FundingType, bool>) f => f.FundingTypeId == 1
    : (Func<FundingType, bool>) f => f.FundingTypeId != 1
return (from a in db.FundingTypes select a).Where(predicate);
Ronald Wildenberg
Don't forget that language specification requires that the type of the conditional operator be known, which requires that at least one of the consequence and the alternative have a type. Lambdas don't have types.
Eric Lippert
Also, you can't just write half a query in there. Where's the "select" that goes with that "from" ?
Eric Lippert
Hm, good points. I had the intention to write the same answer as yours but that didn't really succeed..
Ronald Wildenberg
+11  A: 

That seems perfectly straightforward.

var predicate = include ? 
  (Func<int, bool>) x=>x == 1 : 
  (Func<int, bool>) x=>x != 1 ;
var query = from a in db.FundingTypes where predicate(a.FundingTypeId) select a;
Eric Lippert
Thanks, this worked great!
Jason Heine
Hmm. Firstly, this doesn't compile for me (I needed to add parentheses around each lambda). Secondly (and the reason I tried this out) is that this won't translate to LINQ to SQL et al. Invoking the predicate function within the query introduces a DynamicInvoke into the expression tree.... since this is a query against a "db" I'd have thought you would need something that would translate?
Pete Montgomery
+1  A: 

Hi Jason,

How about this:

var query = 
    from a in db.FundingTypes 
    where (a.FundingTypeId == 1) == include 
    select a;

Joe

This also worked, but I think the Func<,> option provides a bit more flexibility. Thanks!
Jason Heine