开发者

How to use where condition

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 a开发者_高级运维ppened (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


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


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)));


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>


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;
}


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();


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/


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜