tags:

views:

177

answers:

7

can we dynamically appened where condition on a linq query?

for example:

class Result
{
      string v1;
      string v2;
      string v3;
}

List<Result> result = (from r in results select r);


//i want to do something like the following....

if(conditionA)
{
    result = result appened (or v1 = xxx)
}
else if(conditionB)
{
    result = result appened (or v2 = xxx)
}
else if(conditionC)
{
    result = result appened (or v3 == xxx)
}

Anyone know how handle the condition in Linq????

Th

+1  A: 

Just append the Where query operator to your query :

if(conditionA)
{
    result = result.Where(r => r.v1 == xxx);
}
else if(conditionB)
{
    result = result.Where(r => r.v2 == xxx);
}
else if(conditionC)
{
    result = result.Where(r => r.v3 == xxx);
}

Note that your results variable should be declared as IEnumerable<Result>, not List<Result>

Thomas Levesque
how about the relation ship..some is and some is or
shrimpy
oh, right, I didn't notice that.... thekaido's answer should help you then
Thomas Levesque
+1  A: 

You can do this:

if (conditionA)
{
    result = result.Where(p => p.v1 == xxx); // Just guessing at the intent here.
}
// Repeat as necessary...

Or this:

if (conditionA)
{
    result = from r in result where p.v1 == xxx select r;
}
Marcelo Cantos
A: 

As Linq will delay execution, you can just append where to your query, and call tolist in the end to execute:

var query = from r in results;

//i want to do something like the following....

if(conditionA)
{
    query = result.Where(x => x.v1 = xxx);
}
else if(conditionB)
{
    query = result.Where(x => x.v2 = xxx);
}
else if(conditionC)
{
    query = result.Where(x => x.v1 = xxx);
}

List<Result> result = query.ToList();
Luhmann
A: 

Other answers are the simplest solution. If you want a single execution you could also use expression trees:

http://blogs.msdn.com/abhinaba/archive/2006/03/01/541179.aspx http://www.devsource.com/c/a/Languages/Understanding-LINQ-Expression-Trees/1/

Adam Driscoll
A: 

Well, you can always call a function in the where clause, and build your condition there:

...
public bool MeetsConditions(Result r, bool a, bool b)
{
     bool result = false;
     if(a) result = result || r.v1==xxx
     if(b) result = result && r.v2==xxx
     return result;
}
...
var q = select from r in results where MeetsConditions(r, conditionA, conditionB)
m0sa
but i need to dynamically create the condition...the "Result" is a sample..i will have lots of this kind of object
shrimpy
+3  A: 

If you want to build it dynamically, you could use the PredicateBuilder

thekaido
+1 I have used PredicateBuilder for a Specification pattern with great success.
Luhmann
+1  A: 

For an and relationship of clauses, you can easily just append the .Where() filter method, as such:

where conditionOriginal(r) and conditionDynamic(r)

as

var results = (from r in originalResults
               where originalConditions(r)
               select r);
...
if (conditionA)
    results = results.Where(r => conditionDynamic(r));

To append an 'or' type relationship, however, you'd have to union with the original result set, like so:

where conditionOriginal(r) or conditionDynamic(r)

becomes

var results = (from r in originalResults
               where conditionOriginal(r)
               select r);
...
if (conditionB)
    results = results.Union((from r in originalResults
                             where conditionDynamic(r)
                             select r));

or

if (conditionB)
    results = results.Union(originalResults.Where(conditionDynamic(r)));
Tanzelax