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.
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)
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: }
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...
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?
@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.
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:
It's tripping up on the lambda expression though. Debug shows "Expression cannot contain lambda expressions" on that line.
Any ideas why?
Thanks,
Mike
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.
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.
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
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.
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: }
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...
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
The easiest way to find out is to convert your LINQ statement to a plain foreach statement.
@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.
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?
bp.ZoneId = Convert.ToInt32(zone);
Yes, I think you should use Page.LoadControl("~/path");
It's working! I changed the instantiation to...
...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
is working on a reply...