I have a razor view where I get a parameter from query string and then apply a filter using .Where. This parameter can be a single value, or can be a array. Ex: (?eventType=189 or ?eventType=188,189).
For a single value, the code is straight forward and works fine:
var types = HttpUtility.UrlDecode(Request.GetParameterOrDefault("eventType", ""));
var events = Model.Where("eventType.Equals(@0)", types);
However, when it is a array, I couldn't figure out how to solve this. The final result should something like:
var events = Model.Where("eventType.Equals(@0) || eventType.Equals(@1)", type1, type2);
But I don't have a type1, type 2. What I have is "types" which is an string like "188,189" that could be transformed in a string[] of {"188", "189"} using string.Split, but even though the arguments should be placed separetely as the example above.
Can't you check if types is an array or if it's .Length > 1?
(if types is an array or more than one) { events =Model.Where("eventType.Equals(@0) || eventType.Equals(@1)", type1, type2); } else { events =Model.Where("eventType.Equals(@0)", types); }
DynamicNode and Where Clause with array filters.
I had a look at the DynamicNode Documentation particularly on the Where clause.
.Where("condition"[, valueIfTrue, valueIfFalse] )
Returns all items matching the given condition. For more details on queries and conditions, see the section below
I have a razor view where I get a parameter from query string and then apply a filter using .Where. This parameter can be a single value, or can be a array. Ex: (?eventType=189 or ?eventType=188,189).
For a single value, the code is straight forward and works fine:
However, when it is a array, I couldn't figure out how to solve this. The final result should something like:
But I don't have a type1, type 2. What I have is "types" which is an string like "188,189" that could be transformed in a string[] of {"188", "189"} using string.Split, but even though the arguments should be placed separetely as the example above.
Any ideas?
Thanks.
Just a suggestion... :)
Can't you check if types is an array or if it's .Length > 1?
It's an idea :)
Thank you for your suggestion Brett, but I don't have type1, type2, etc. I have only a array of type.
I noticed I'm interacting with DynamicNodeList instead of DynamicNode.
DynamicNodeList has a method ".Where" with this signature:
If I use the code below, it works fine (returns 2 results):
But if I use the following code, it does not work (returns no results).
However, it should work in the same way, according to C# documentation about params:
http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.71).aspx
I ended up using this code:
But I would prefer using placeholders.
is working on a reply...