Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Having trouble with figuring out the proper use of .SingleOrDefault()
I have two strings: collect and type
I need to check item(s) have the .Name equaling collect and also check a property value of it equals type
At the moment I have this:
CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect);
This works fine, but I need to include the "and has property value of type"
I was working with the assumption of this:
CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && GetPropertyValue(propertyName) == type);
try this
CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && x.GetPropertyValue<string>(propertyName) == type);
Thanks Ali - I'm getting this error now: The non-generic method 'umbraco.MacroEngines.DynamicNode.GetPropertyValue(string)' cannot be used with type arguments Here's a little bit more code:
string collect = String.Format("{0}", Request.QueryString["Bottle"]); string type = String.Format("{0}", Request.QueryString["Type"]);DynamicNode rep = null;
rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && x.GetPropertyValue<string>(recipeType) == type);
I see you are using umbraco 4.
can you please try this (removed <string>)
<string>
CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect && x.GetPropertyValue(propertyName) == type);
oo, getting close!
I'm now getting an error
Exception: System.NullReferenceException: Object reference not set to an instance of an object.
from:
for (i = 0; i < rep.Children.Count() / numInRow; i++) {
a loop to do something to each of the collected items in rep. Can I not use .Count() anymore?
I am running Umbraco v6.1.3 (Assembly version: 1.0.4954.19342)
it seems that rep is null and you are trying to get the children of it.
so you should check for null before loop
if(rep != null) { for (i = 0; i < rep.Children.Count() / numInRow; i++) { // your logic } }
rep =CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name== collect && x.GetPropertyValue<string>(recipeType)== type);
Shouldn't this populate rep so that it's no longer null?
it depends on your data to be honest.
sorry but I'm completetly confused now.
If I define DynamicNode rep = null;
then define rep = a collection of items
Why would it matter what type of data it is?
the problem here is that the "a collection of items" are null.
to be frank it is really difficult as I am not sure what you would like to do and what data structure you have in Umbraco
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
singleOrDefault syntax issue
Having trouble with figuring out the proper use of .SingleOrDefault()
I have two strings: collect and type
I need to check item(s) have the .Name equaling collect and also check a property value of it equals type
At the moment I have this:
This works fine, but I need to include the "and has property value of type"
I was working with the assumption of this:
try this
Thanks Ali - I'm getting this error now:
The non-generic method 'umbraco.MacroEngines.DynamicNode.GetPropertyValue(string)' cannot be used with type arguments
Here's a little bit more code:
I see you are using umbraco 4.
can you please try this (removed
<string>
)oo, getting close!
I'm now getting an error
from:
a loop to do something to each of the collected items in rep. Can I not use .Count() anymore?
I am running Umbraco v6.1.3 (Assembly version: 1.0.4954.19342)
it seems that rep is null and you are trying to get the children of it.
so you should check for null before loop
Shouldn't this populate rep so that it's no longer null?
it depends on your data to be honest.
sorry but I'm completetly confused now.
If I define DynamicNode rep = null;
then define rep = a collection of items
Why would it matter what type of data it is?
the problem here is that the "a collection of items" are null.
to be frank it is really difficult as I am not sure what you would like to do and what data structure you have in Umbraco
is working on a reply...