Copied to clipboard

Flag this post as spam?

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


  • Fuji Kusaka 2203 posts 4220 karma points
    May 21, 2013 @ 13:06
    Fuji Kusaka
    0

    Take() not working with DynamicNode

    Can someone explain why Take() isnt working here ? 

    var SpecialOfferFdr = @Model.AncestorOrSelf(2).Descendants("v").Where("Visible").Items;

    foreach(dynamic t in SpecialOfferFdr.Take(1)){ 
    if(t.Level == 4){ @t.Name } }

    This didnt work either 

    var SpecialOfferFdr = @Model.AncestorOrSelf(2).Descendants("v").Where("Visible").Take(1).Items;

    foreach(dynamic t in SpecialOfferFdr){ 
    if(t.Level == 4){ @t.Name } }

    //fuji

  • Stephen 767 posts 2273 karma points c-trib
    May 21, 2013 @ 13:26
    Stephen
    0

    What do you mean by "isn't working"?

  • Fuji Kusaka 2203 posts 4220 karma points
    May 21, 2013 @ 18:28
    Fuji Kusaka
    0

    Hi Stephen,

    Well when i make use of Take(2) here

    var SpecialOfferFdr = @Model.AncestorOrSelf(2).Descendants("v").Where("Visible").Take(2).Items;

    foreach(dynamic t in SpecialOfferFdr){   
           if(t.Level == 4){                                                                                                                      @t.Parent().Name
              }
    }

    I dont get any results,.

    And if i make use of this

    foreach(dynamic t in SpecialOfferFdr.Take(2)){    
           if(t.Level == 4){                                                       
                @t.Parent().Name                   
            }
    }

    I get an error when rendering the script

     

  • Stephen 767 posts 2273 karma points c-trib
    May 21, 2013 @ 18:54
    Stephen
    101

    In the second case that is because SpecialOfferFdr is obtained from DynamicNodeList.Items so it's an IEnumerable<DynamicNode>... and .Take(2) is an extension method of IEnumerable<T>... trouble is, because Model is dynamic, SpecialOfferFdr is dynamic, and extension methods don't run on dynamics. If you do:

    IEnumerable<DynamicNode> SpecialOfferFdr = Model.....Items;
    foreach (dynamic t in sof.Take(2)) { ... }

    It should work because then SpecialOfferFdr is not a dynamic but explicitely an IEnumerable<T> and the Take() extension method works.

    As for why the first case does not work... can't tell...

  • Fuji Kusaka 2203 posts 4220 karma points
    May 21, 2013 @ 19:01
    Fuji Kusaka
    0

    Yes got working i changed  it to

    List<DynamicNode> SpecialOfferFdr=@Model.AncestorOrSelf(2).Descendants("v").Where("Visible").Take(2).Items;
  • Fuji Kusaka 2203 posts 4220 karma points
    May 21, 2013 @ 19:04
    Fuji Kusaka
    0

    Well tried both and works. Thanks for the tip

    List<DynamicNode> 
    IEnumerable<DynamicNode>
Please Sign in or register to post replies

Write your reply to:

Draft