Copied to clipboard

Flag this post as spam?

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


  • Mike Taylor 155 posts 353 karma points
    Oct 19, 2012 @ 13:03
    Mike Taylor
    0

    Why is that my "Expression cannot contain lambda expressions."?

    I'm writing a user control for a custom section in the back-end.

    I'm passing a node ID to my user control via a public property, and all I'm trying to do (for now) is bind a simple repeater to show the IDs of all of that node's child documents (not node factory, as I need to see non-published nodes too) that have certain doctypes.

    My code looks like this:

    private int nodeId;
    public int NodeId
    {
        get { return nodeId; }
        set { nodeId = value; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        var thisDoc = new Document(NodeId);
        if (thisDoc.HasChildren)
        {
            rptTitles.DataSource = thisDoc.Children.OfType<Document>().Where(x => x.ContentType.Alias == "Title" || x.ContentType.Alias == "Digital");
            rptTitles.DataBind();
        }
    }

     

    It's tripping up on the lambda expression though. Debug shows "Expression cannot contain lambda expressions" on that line.

    Any ideas why?

    Thanks,

    Mike

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 19, 2012 @ 13:15
    Lee Kelleher
    0

    Hi Mike,

    Are you using uComponents or Umbraco 4.8+? If so, you can make use of uQuery to get the child nodes using LINQ/lambda.

    var thisDoc = new Document(NodeId);
    if (thisDoc .HasChildren)
    {
        var aliases = new string[] { "Title", "Digital" };
        rptTitles.DataSource = thisDoc.GetChildDocuments(x => aliases.Contains(x.ContentType.Alias));
        rptTitles.DataBind();
    }

    I switched around the test for the DocType aliases... just find that easier than many OR statements (if you're checking the same value)

    Cheers, Lee.

  • Mike Taylor 155 posts 353 karma points
    Oct 19, 2012 @ 13:25
    Mike Taylor
    0

    Hiya Lee.

    Yeah, using 4.9.0, so have been trying various uQuery methods similar to yours above.

    It's still tripping up on the lambda though - see screencast of me debugging it with the code you posted... http://screencast.com/t/SRHcbdvoN

    Been trying to get child documents *all morning* ... (sigh) :-)

    Mike

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 19, 2012 @ 13:29
    Lee Kelleher
    0

    Does it work outside of debugging it?

    Just thinking that it could be an issue with Visual Studio's debugger rather than the code itself.

  • Mike Taylor 155 posts 353 karma points
    Oct 19, 2012 @ 13:30
    Mike Taylor
    0

    No, I get this exciting error page:

     

    Server Error in '/' Application.


    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error: 

    Line 31:             {
    Line 32:                 var aliases = new string[] { "Title", "Digital" };
    Line 33: rptTitles.DataSource = thisDoc.GetChildDocuments(x => aliases.Contains(x.ContentType.Alias)); Line 34:                 rptTitles.DataBind();
    Line 35:             }


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Oct 19, 2012 @ 13:32
    Stefan Kip
    0

    It's not possible to use lamba expressions at debugging time: http://stackoverflow.com/questions/4783085/c-sharp-debugging-functions-that-contain-lambda-expressions

    That's why you're getting the error.
    It sounds to me that the original error (Object null ref) means one of your Children has a ContentType or Alias which is null...

  • Mike Taylor 155 posts 353 karma points
    Oct 19, 2012 @ 13:34
    Mike Taylor
    0

    That's not possible though, is it?

    I have a particular node which has two sub-nodes, both of which are of the same doctype ("Title" in this instance). How could a node have a null ContentType or Alias?

    M

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Oct 19, 2012 @ 13:35
    Stefan Kip
    1

    The easiest way to find out is to convert your LINQ statement to a plain foreach statement.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 19, 2012 @ 13:35
    Lee Kelleher
    0

    @Mike - just want to rule out something... when debugging, is "rptTitles" initialised?

    I'd suggest assigning the child docs to its own variable, then assign that to the repeater's DataSource ... see if it's any help in pinpointing the issue.

  • Mike Taylor 155 posts 353 karma points
    Oct 19, 2012 @ 13:59
    Mike Taylor
    0

    Hmm... I think you're right - it looks like rptTitles is null.

    It's just a standard <asp:Repeater> control declared in the user control though. Why would it be null in the Page_Load event of that user control?

    Could it be to do with the fact that I'm instantiating the user control programatically?

    BulkPricing bp = newBulkPricing();
    bp.NodeId = Convert.ToInt32(Request.QueryString["id"]);
    bp.ZoneId = Convert.ToInt32(zone);
  • Stefan Kip 1614 posts 4131 karma points c-trib
    Oct 19, 2012 @ 14:10
    Stefan Kip
    0

    Yes, I think you should use Page.LoadControl("~/path");

  • Mike Taylor 155 posts 353 karma points
    Oct 19, 2012 @ 15:18
    Mike Taylor
    1

    It's working! I changed the instantiation to...

    var bp = (BulkPricing)Page.LoadControl("/usercontrols/Education/BulkPricing.ascx");


    ...and it all works!

    Round the houses a bit, but it's all good learning. Thanks so much for all your help Lee & Stefan.

    Mike

Please Sign in or register to post replies

Write your reply to:

Draft