Copied to clipboard

Flag this post as spam?

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


  • Tom 738 karma points
    Jul 21, 2011 @ 00:40
    Tom
    0

    No applicable method 'ContainsAny' exists in type 'String

    I was just wondering in this instance why the extension method wasn't being picked up.. and for now is there a workaround:

     

    No applicable method 'ContainsAny' exists in type 'String

    when trying to do the filter on @Model.Parent.Children in the line below where I'm setting the var: remainingProductsInCollection..

     

    Is anyone aware of a way to work around this?

     

     var valuesForFilter new Dictionary<stringobject>();

     

      var productCodes new List<string>();

      foreach (var selectedProduct in recommendedProducts)
      {
        productCodes.Add(selectedProduct.Name);
      }

      if (productCodes.Count 0)
      {
        valuesForFilter.Add("productCodes"productCodes);
        var remainingProductsInCollection @Model.Parent.Children.Where("!Name.ContainsAny(productCodes)"valuesForFilter);

        if (remainingProductsInCollection != null &remainingProductsInCollection.Items.Count 0)
        {
          var remainingProductsToAdd remainingProductsInCollection.Items.Count recommendedProductsCount remainingProductsInCollection.Items.Random(recommendedProductsCountremainingProductsInCollection.Items;
          recommendedProducts.AddRange(remainingProductsToAdd);
        }
      }

     

  • Gareth Evans302 karma points
    Jul 21, 2011 @ 00:50
    Gareth Evans
    0

    Does *this* work:

    var remainingProductsInCollection = @Model.Parent.Children.Where("Name.ContainsAny(productCodes)", valuesForFilter);

    The reverse case, no ! before "Name"
    Also can you try on a non default property, i.e. a property added to the doc type.
    It sounds like the expression tree is missing a couple of if statements when dealing with the lambda (this is something for me to fix, but I am trying to get some more accurate debugging info about what cases it fails with and which ones it works with)

  • Tom738 karma points
    Jul 21, 2011 @ 01:22
    Tom
    0

    Hi Gareth,

    unfortunately that does not work.. hmm.. if I try it on a non default property i get sequence contains no matching element as an exception.. but that looks like it's kicking it in to gear..

     

    is there a workaround for this kind of functionality I could use for now.. 

     

    In Extension Method Finder Line 130: firstMethod = methods.First(x => x.IsGenericMethodDefinition); that is where it is blowing up if I try on a non default

  • Gareth Evans302 karma points
    Jul 21, 2011 @ 01:26
    Gareth Evans
    0

    Okay, that's progress though, it suggests there's two bugs with ContainsAny

    The workaround would be to write a class library with your static extension method you want accessible with the reverse of ContainsAny (e.g. NotContainsAny), I think that was documented in part 3 as well, but referenced in part 5.

    I am not sure what's throwing the sequence contains no matching element.

    The other possible workaround would be a longhand where, for example:

    .Where("Name != "blah" && Name != "blah2") but you'd need to know the values

    Or just have a standard contains check as an if statement inside your loop (messy but a workaround always is)

    Gareth

     

  • Gareth Evans302 karma points
    Jul 21, 2011 @ 01:28
    Gareth Evans
    0

    Re:

    In Extension Method Finder Line 130: firstMethod = methods.First(x => x.IsGenericMethodDefinition); that is where it is blowing up if I try on a non default

    That suggests that "methods" didn't find any methods by that name.. odd.

    I'll change that First to be FirstOrDefault and null check for the 4.7.1 beta

  • Tom738 karma points
    Jul 21, 2011 @ 01:32
    Tom
    0

    Cheers Gareth,

    hmm might have to write an extension method and see how I go.. please keep me posted on this as it's a really awesome feature.. ill get one of the guys to have a look here with me this morning to see if we can track down what it's doing..

     

    I wonder if I could take one of your contains any methods from the source code and move it into an extension..

  • Tom738 karma points
    Jul 21, 2011 @ 02:20
    Tom
    0

    Hi Guys,

    Just thought I'd post a workaround for you

    At Gareth's suggestion I made an extension method.. with an overload.. so!

    public static DynamicNodeList NotContainsAny(this DynamicNodeList all, List<string> valuesForComparison)

    {

    var items = all.Items.Where(item => !valuesForComparison.Contains(item.Name));

    return new DynamicNodeList(items);

    }

     

    public static DynamicNodeList NotContainsAny(this DynamicNodeList all, string propertyName, List<string> valuesForComparison)

    {

    var items = all.Items

    .Where(

    item =>

    {

    var property = item.GetProperty(propertyName);

    return (property != null) && !valuesForComparison.Contains(property.Value);

    }

    );

     

    return new DynamicNodeList(items);

    }

  • Gareth Evans302 karma points
    Jul 21, 2011 @ 02:33
    Gareth Evans
    0

    Awesome Tom,

    By the way, does anyone else actually use this feature ;)

     

    Gareth

  • Tom738 karma points
    Jul 21, 2011 @ 02:34
    Tom
    0

    haha :)

  • Gareth Evans302 karma points
    Jul 22, 2011 @ 01:31
    Gareth Evans
    0

    Fixed in 4.7.1

  • Tom738 karma points
    Jul 22, 2011 @ 01:41
    Tom
    0

    Thanks that'd be great I'll try the binary.. but im running 4.7.0..

    Cheers,

    Tom

  • Gareth Evans302 karma points
    Jul 22, 2011 @ 01:47
    Gareth Evans
    0

    I am running two 4.7.0 sites where i've just "half upgraded" (by replacing the binaries) the site to 4.7.1 to test razor features.
    A few things are probably broken as the aspx/ascx/asmx/css/js etc files should also be updated.
    I don't recommend this for a production site,
    This build is current as of 2011-07-22 and I will periodically update it
    http://dl.dropbox.com/u/2923715/LatestRazorMacroEngine.zip

    Gareth

  • William Chang26 karma points
    Jul 22, 2011 @ 10:06
    William Chang
    0

    @Gareth Evans

    FYI, I use "ContainsAny" in Razor C# and I think it is really nice to have this feature working.

    Thus, thank you Gareth Evans for your help fixing/patching the "ContainsAny", I will test your patch. Much appreciated. :)

    I already posted the same issue weeks ago:
    http://our.umbraco.org/forum/developers/razor/19767-Trying-to-use-containsAny-in-Razor-in-47 

Please Sign in or register to post replies

Write your reply to:

Draft
Our.umbraco.org is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and a ton of packages from the community.