I have this, which works. But can it be written simpler.
I wanna select 1 node id into 'hazardousMaterialId', based on what i get from the variable called 'Query'
hazardousMaterialPages = hazardousMaterialPages.Where(
x => x != null && x.IsVisible()
&& (
x.Name.ToLower(CultureInfo.InvariantCulture).Contains(Query)
||
x.GetPropertyValue<string>("fullName").ToLower(CultureInfo.InvariantCulture).Contains(Query)
)
);
string hazardousMaterialId = string.Empty;
foreach(var Page in hazardousMaterialPages)
{
hazardousMaterialId = Page.Id.ToString();
}
Oh I get it now. If you are expecting only one item, you actually don't need a for loop. You can use the First() or FirstOrDefault() functions to get the first item directly from your query.
Optimize where staement
Hi,
I have this, which works. But can it be written simpler. I wanna select 1 node id into 'hazardousMaterialId', based on what i get from the variable called 'Query'
How about this part instead of your "or" part:
I don't think it can get much simpler though, I'd also like to see if anyone has anything else to suggest.
Hi,
Thanks, ill try that.
I thought that this part, could be addede directly to the where statement somehow.
foreach(var Page in hazardousMaterialPages) {
hazardousMaterialId = Page.Id.ToString();
}
As far as I understand you are asking whether you can include your whole where clause inside the foreach statement. If so, you can do that, like this:
No,
rewrite the for loop, to be a direct part of the where statement.
The code only select one item as it is now.
Oh I get it now. If you are expecting only one item, you actually don't need a for loop. You can use the First() or FirstOrDefault() functions to get the first item directly from your query.
I would suggest the following:
Hi Jeavon,
Also nice, i wil use this: InvariantContains. Do i need a ToLower() and then InvariantContains?
Hi Sotiris,
I i do like this?
hazardousMaterialPages = hazardousMaterialPages.Where( x => x != null && x.IsVisible() && (string.Concat(x.Name, " ", x.GetPropertyValue("fullName")).ToLower().InvariantContains(Query)) ).FirstOrDefault();
The above gives me this error:
Cannot implicitly convert type 'Umbraco.Core.Models.IPublishedContent' to 'System.Collections.Generic.IEnumerable
How will i get the id of the selection into my variable?
Something like this?
hazardousMaterialId = hazardousMaterialPages.Id;
No you don't need ToLower with InvariantContains, lots of great Invariant methods in the Umbraco.Core.StringExtensions class!
Got it.
hazardousMaterialPages = hazardousMaterialPages.Where( x => x != null && x.IsVisible() && (string.Concat(x.Name, " ", x.GetPropertyValue("fullName")).ToLower().InvariantContains(Query)) );
hazardousMaterialId = @hazardousMaterialPages.FirstOrDefault().Id;
Down to this:
hazardousMaterialId = Umbraco.TypedContent(1696).Descendants().Where(x => x != null && x.IsVisible() && (string.Concat(x.Name, " ", x.GetPropertyValue("fullName")).ToLower().InvariantContains(Query))).FirstOrDefault().Id.ToString();
But in case the query dosnt have any results , i need this:
Is there an easier way for this?
is working on a reply...