Copied to clipboard

Flag this post as spam?

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


  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Jun 25, 2011 @ 22:54
    Thor Madsen-Holm
    0

    Problem comparing Id's within a Where()

    Hi, 

    I've encountered yet another problem on my quest to learn Razor :-). 
    I am trying to fetch the id of a page using some Where() clauses, but i cant get my code working. Here is what i have:

    <h1>@Model.AncestorOrSelf().Descendants().Where("rightContent").Where("Id != Model.Id").Last().Id</h1>

    This is the error i get: 

    Error loading Razor Script [KV][Razor]Dokument.cshtml
    No property or field 'Id' exists in type 'Func`2'

    I did some trial and error testing and found that it is the last Where("Id != Model.Id") which is causing the error.
    I tried some other appoaches but all failed, so i am pretty stuck at the moment. 

    Does anyone have any idea how to go around problem? 
    Any help will be much appreciated 

    /Thor 

  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Jun 26, 2011 @ 11:53
    Thor Madsen-Holm
    1

    Eureka! I managed to fix the issue myself. 
    @Model.Id just had to be "outside" of the quotes. 
    Here is how that looks: 

    <h1>@Model.AncestorOrSelf().Descendants().Where("rightContentInherit").Where("Id != " + @Model.Id).Last().Id</h1>

    /Thor

  • Sebastiaan Janssen 4850 posts 14395 karma points MVP admin hq
    Jun 26, 2011 @ 12:02
    Sebastiaan Janssen
    1

    I've tested similar code and get the same error, seems like a bug:

    @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Id != Model.Id"))
    {
        <li>@page.Name</li>
    }

    For now what you could do is create a list of Id's and get is from there:

     

    @{ 
    var IdList = new List<int>();
    foreach (var page in Model.AncestorOrSelf().Descendants().Where("rightContent"))
    {
    IdList.Add(page.Id);
    }
    <h1>@IdList.Where(x => x != Model.Id).Last()</h1>
    }

     

  • Thor Madsen-Holm 82 posts 212 karma points c-trib
    Jun 26, 2011 @ 12:13
    Thor Madsen-Holm
    0

    Thanks for the input Sebastian, I will try out your snippet :-)

  • Sebastiaan Janssen 4850 posts 14395 karma points MVP admin hq
    Jun 26, 2011 @ 12:16
    Sebastiaan Janssen
    0

    Good to see that you found an easier solution! Too bad that you can't mark your own answers as the solution (..yet! this is on the list of things to fix on "Our").

  • Mads Krohn 211 posts 501 karma points c-trib
    Jun 26, 2011 @ 12:50
    Mads Krohn
    1

    In cases like this, where the queries can get somewhat complex, it might be easier for you to cast the Model object to a DynamicNode. In that way, you'll be able to use "real linq" and avoid some of the oddities in using dynamic expressions. Following is just an example.

    @{
    var node = Model as DynamicNode;


    @node.AncestorOrSelf().Descendants().Items.Last(x => x.NodeTypeAlias == "SomeThing").Name; 

    Just a suggestion :)

Please Sign in or register to post replies

Write your reply to:

Draft