views:

171

answers:

2

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?

A: 

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

recursive
A: 

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");
p.campbell
The problem in using this method is that I don't know which of about 8 different "foovalue" or "barvalue" I will be using in the common control. Hence adding the whereparameter dynamically rather than pre-declaring it.I may see if I can add two generic values and then change the name of the property at runtime...
Carl
@Carl: update to show how to modify the where and parameters at runtime.
p.campbell
@Carl: did this update help you? If you need any more help, please comment. Please remember to upvote and 'accept answers' as part of the StackOverflow community!
p.campbell