Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Francisco 21 posts 72 karma points
    Mar 12, 2013 @ 03:46
    Francisco
    0

    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

    @var ancestors = Model.Where("UmbracoNaviHide != @0", "True");
    
    @var HomePage = Model.Where("NodeTypeAlias == @0 && Level == @1 || Name = @2", "HomePage", 0, "Home").First();

     

    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.

    Any ideas?

    Thanks. 

     

     

     

  • Brett Hadley 16 posts 36 karma points
    Mar 12, 2013 @ 14:42
    Brett Hadley
    0

    Just a suggestion... :)

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

     

    It's an idea :)


  • Francisco 21 posts 72 karma points
    Mar 12, 2013 @ 22:13
    Francisco
    0

    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:

    public IQueryable Where(string predicate, params object[] values)

    If I use the code below, it works fine (returns 2 results):

    events = events.Where("eventType == @0 || eventType == @1", 188, 189)

    But if I use the following code, it does not work (returns no results).

    events = events.Where("eventType == @0 || eventType == @1", new[] { 188, 189 });

    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

  • Francisco 21 posts 72 karma points
    Mar 13, 2013 @ 03:37
    Francisco
    100

    I ended up using this code:

    events = events.Where("eventType == 188 || eventType == 189");

    But I would prefer using placeholders.

     

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies