Carl
Carl

Reputation: 161

LinqDataSource dynamic parameters - forcing an OR with WhereParameters

In my "selecting" statement I need to add two dynamic parameters when using the LinqDataSource's WhereParameters collection:

e.WhereParameters.Add(param, True)

However, the system adds these parameters as AND, but I want to perform an OR where either parameter 1 OR parameter 2 is true.

How can this be done?

Upvotes: 1

Views: 4199

Answers (2)

p.campbell
p.campbell

Reputation: 100567

If you don't know the number of params at design time, and can only determine at runtime, consider changing the Where property of the datasource at runtime. You can insert actual values yourself.

 //e.g. we know that we want 4 params this time.
 LinqDataSource1.Where = 
    "Alpha == 1 OR Brave==False OR Charlie> 'Jan 1 1999' or Delta = @DeltaVal";
 LinqDataSource1.WhereParameters.Add("DeltaVal", "O'Flanagan");

If you do know the n params at runtime, you could simply put the OR statement at design-time, and modify their values.

Perhaps put your OR clause right in the LinqDataSource declaration.

<asp:LinqDataSource ID="LinqDataSource1" runat="server"          
     Where="Foo == @FooValue OR Bar==@BarValue">

Then you code-behind can add those two where parameters.

LinqDataSource1.WhereParameters.Add("FooValue", "milk");
LinqDataSource1.WhereParameters.Add("BarValue", "eggs");

Upvotes: 2

recursive
recursive

Reputation: 86084

You could use Joe Albahari's PredicateBuilder. I've used it with great success.

Upvotes: 0

Related Questions