Search code examples
c#umbracoumbraco7

Exclude a set of nodes picked in multinode tree picker from a Descendants Where() clause in Umbraco 7


I've setup a multinode treepicker to pick a set of nodes which I'm trying to exclude from a set of Descendants nodes.

I'm not sure about the Where() syntax to accomplish this:

var exclude_nodes = CurrentPage.pickedNodes;

var nodes = Model.Content.AncestorsOrSelf("homepage").First().Descendants("addonProduct").Where( filter out exclude_nodes here);

Solution

  • Would this work for you?

    new [] { 1, 2, 3 }.Where(x => x > 1) // { 2, 3 }
    new [] { 1, 2, 3 }.Except(new [] { 2, 3 }) // { 1 }
    

    i.e. in your case

    var nodes = ...Descendants("addonProduct").Except(exclude_nodes);
    var nodes = ...Descendants("addonProduct").Where(d => !exclude_nodes.Contains(d));
    

    Please note Except() behavior:

    new [] { 1, 1, 2, 3 }.Except(new [] { 2 }) // { 1, 3 }