Copied to clipboard

Flag this post as spam?

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


  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 08:36
    Peter Cort Larsen
    0

    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'

    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();
    
    }
    
  • Sotiris Filippidis 274 posts 1395 karma points
    Feb 08, 2017 @ 08:52
    Sotiris Filippidis
    0

    How about this part instead of your "or" part:

    (string.Concat(x.Name, " ", x.GetPropertyValue("fullName")).ToLower(CultureInfo.InvariantCulture)).Contains(Query)
    

    I don't think it can get much simpler though, I'd also like to see if anyone has anything else to suggest.

  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 09:10
    Peter Cort Larsen
    0

    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();

    }

  • Sotiris Filippidis 274 posts 1395 karma points
    Feb 08, 2017 @ 09:12
    Sotiris Filippidis
    0

    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:

    foreach(var Page in hazardousMaterialPages.Where(...)) {
    
  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 09:22
    Peter Cort Larsen
    0

    No,

    rewrite the for loop, to be a direct part of the where statement.

    The code only select one item as it is now.

  • Sotiris Filippidis 274 posts 1395 karma points
    Feb 08, 2017 @ 09:27
    Sotiris Filippidis
    100

    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.

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Feb 08, 2017 @ 09:34
    Jeavon Leopold
    0

    I would suggest the following:

    hazardousMaterialPages = hazardousMaterialPages.Where(x => x != null 
                                                        && x.IsVisible() 
                                                        && (x.Name.InvariantContains(Query) 
                                                        || x.GetPropertyValue<string>("fullName").InvariantContains(Query)));
    
  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 10:14
    Peter Cort Larsen
    0

    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;

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Feb 08, 2017 @ 10:30
    Jeavon Leopold
    1

    No you don't need ToLower with InvariantContains, lots of great Invariant methods in the Umbraco.Core.StringExtensions class!

  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 10:17
    Peter Cort Larsen
    0

    Got it.

    hazardousMaterialPages = hazardousMaterialPages.Where( x => x != null && x.IsVisible() && (string.Concat(x.Name, " ", x.GetPropertyValue("fullName")).ToLower().InvariantContains(Query)) );

    hazardousMaterialId = @hazardousMaterialPages.FirstOrDefault().Id;

  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 10:21
    Peter Cort Larsen
    0

    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();

  • Peter Cort Larsen 388 posts 922 karma points
    Feb 08, 2017 @ 10:28
    Peter Cort Larsen
    0

    But in case the query dosnt have any results , i need this:

    hazardousMaterialPages = hazardousMaterialPages.Where(x => x != null && x.IsVisible() && (string.Concat(x.Name, " ", x.GetPropertyValue("fullName")).ToLower().InvariantContains(Query)));
    
    if (hazardousMaterialPages.FirstOrDefault() != null) {
    
    hazardousMaterialId = hazardousMaterialPages.FirstOrDefault().Id.ToString();
    
    }
    

    Is there an easier way for this?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies