Copied to clipboard

Flag this post as spam?

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


  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 10:03
    Lewis
    0

    C# error when using .Where() on CurrentModel.Children

    Hi all.

    In a line like this in a razor file:

    var myVar = CurrentModel.Children.Where(x => x.GetPropertyValue("myProperty") == "myValue");

    I get the following errors:

    The type arguments for method 'umbraco.MacroEngines.DynamicNodeList.Where<T>(string, params object[])' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    and

    One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll

    I don't get any intellisense for x in the lamda expression. 

    This only happens in the presentation project, in the website project I get intellisense and no errors are thrown.

    It builds every other time, and when it does the code runs as expected.

    I am not missing references to the dlls mentioned above I have tried cleaning and rebuilding, I have tried deleting the temporary .NET files (which actually solved the problem for a short while) but now the errors are back and I am out of ideas.

    If anyone has any ideas as to why this could be happening please share, it would be greatly appreciated!

    Thanks,
    Lewis


  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 10:04
    Lewis
    0

    Should have mentioned, using Umbraco 6.1.5

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 05, 2013 @ 10:14
    Jeavon Leopold
    0

    Hi Lewis,

    Could you please post your entire script, especially the definition of CurrentModel?

    Thanks,

    Jeavon

  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 10:21
    Lewis
    0

    @using umbraco.MacroEngines

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{  

        <ul>

        @foreach (DynamicNode n in CurrentModel.AncestorsOrSelf().Where(x => x.GetPropertyValue("umbracoNaviHide") != "1"))

        {

            if(CurrentModel.Id != n.Id)

            {

                <li><a href="@n.Url">@n.Name</a></li>

                continue;

            }

            <li>n.Name</li>

        }

        </ul>

    }

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 05, 2013 @ 10:26
    Peter Gregory
    0

    Because you cant write linq against a dynamic.

    Im assuming that this is a Razor scripting file and not a Partial?

    Your query should be

    var myVar = Model.Children.Where("myProperty" == \"myValue\"");

    or use something like uQuery or Node.

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 05, 2013 @ 10:28
    Peter Gregory
    0

    Edit on that last one.

    var myVar = Model.Children.Where("myProperty == \"myValue\"");

    One too many quotes.

  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 10:30
    Lewis
    0

    The thing is I'm not using Model I am using CurrentModel, which is of type DynamicNode.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 05, 2013 @ 10:33
    Jeavon Leopold
    0

    Where are you getting CurrentModel from?

  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 10:37
    Lewis
    0

    By inheriting DynamicNodeContext.

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 05, 2013 @ 10:40
    Peter Gregory
    1

    Yep Sorry I missed that you were using CurrentModel and not Model. Jeavon CurrentModel is kinda equivalent to Node.

    This works for me.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    
    @{
        var col = CurrentModel.Children.Where(x=> x.GetPropertyValue("something") == "test");
    }
    
    @foreach(var item in col){
        <p>@item.GetPropertyValue("something")</p>
    }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 05, 2013 @ 10:50
    Jeavon Leopold
    0

    Peter, just found it, I never knew that existed! All this time I have done new DynamicNode(Model.Id) and could have just used CurrentModel! Never mind, have moved onto Mvc now :-)

    Lewis, any reason you are using Razor Macro rather than Mvc?

  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 10:53
    Lewis
    0

    No particular reason, but we are too far into the build on this project to make the switch.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 05, 2013 @ 10:58
    Jeavon Leopold
    0

    Cool, you can always use a "Partial View Macro" within your MasterPages if you would like to make use of the new PublishedContent rather than the legacy DynamicNode :-)

  • Lewis 36 posts 116 karma points
    Sep 05, 2013 @ 12:50
    Lewis
    0

    Thanks for the suggestion, it may be something we look into doing. However the original issue still remains a mystery. Why does the code work in one project but not another? Anyone have any ideas?

  • Lewis 36 posts 116 karma points
    Sep 06, 2013 @ 14:14
    Lewis
    0

    Peter - the code works for me in other projects, just not this one.

Please Sign in or register to post replies

Write your reply to:

Draft