-7

I have structure ifs:

if()
{
  query = query.Where(....)
}

if()
{
   query = query.Where(....)
}

as they do to these two tests work together, and do not overwrite each other?

Mediator
  • 14,951
  • 35
  • 113
  • 191
  • You can add && operator within Where claus – Danilo Vulović Oct 01 '12 at 09:16
  • 4
    -1. Please word ur question properly – nawfal Oct 01 '12 at 09:17
  • 8
    @nawfal - Yes, like using 'your' instead of 'ur'. – H H Oct 01 '12 at 09:19
  • Do you mean that you want to complete a where clause according to external conditions? (like for instance if "the user is anonymous" his search will be further restricted) – Eilistraee Oct 01 '12 at 09:20
  • 2
    Hi, your question is getting downvoted because it is very poorly worded and no effort has been made. Try to improve it and you will get a better response – James Wiseman Oct 01 '12 at 09:21
  • "*do not overwrite each other*" - The queries aren't "overwriting" eachother they are "chaining" together, what you are doing is fine considering you have specific conditions to chain. It's the equivalent of doing `query.Where(...).Where(...)`. – James Oct 01 '12 at 09:21
  • See [How to build up a Linq to Sql where clause bit by bit?](http://stackoverflow.com/questions/898286/how-to-build-up-a-linq-to-sql-where-clause-bit-by-bit), [VB.Net Linq - How to append a where clause?](http://stackoverflow.com/questions/782566/vb-net-linq-how-to-append-a-where-clause), and [How do I implement a dynamic 'where' clause in LINQ?](http://stackoverflow.com/questions/1001462/how-do-i-implement-a-dynamic-where-clause-in-linq). – GSerg Oct 01 '12 at 09:27

4 Answers4

2

as they do to these two tests work together, and do not overwrite each other?

Your code will (depending on the outcome in the if()s) chain the queries.

The final result will be that all .Where(...) s are applied to the source.

H H
  • 263,252
  • 30
  • 330
  • 514
1

You should be able to chain your query expression:

query = query.Where(....).Where(....);
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
1

If you want to have some flexibility in predicate creation, I'd suggest LinqKit. With this library you can build your Where clause dynamically, to get, for example:

if(condition)
{
   predicate.And(something);
}
if(otherCondition)
{
   predicate.Or(somethingElse);
}
var result = source.Where(predicate);

It has open source code, very easy to understand, basing on Expression class, which is actually used by Linq.

It's also worth noticing, that the result is computed only when it's needed - so it's a good solution to create a condition for Linq2SQL.

Piotr Zierhoffer
  • 5,005
  • 1
  • 38
  • 59
0
var flg1 = cond1 == null? true : cond1;
var flg2 = cond2 == null? true : cond2;

var result = query.Where(flg1 && flg2)

You also can use the DynamicQuery library. Then you can use like

query.Where("field1=1 and field2=2");
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
  • Please show how this is going to work when `cond1` and `cond2` are sql conditions that refer to data tables used in the query. – GSerg Oct 01 '12 at 09:38