Copied to clipboard

Flag this post as spam?

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


  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 08:17
    Nadine Fisch
    0

    Filter noderesults

    Hi,

    I have a "list" of nodes and I want to filter them for a specific property value. The property value can contain the searchterm or it can be equal. I look for something that is similiar to a sql statement, where you would use the LIKE-Syntax.

    SELECT field FROM table LIKE '%@searchterm%'
    

    At the moment my razor syntax looks like

    nodes.Where("contentHeadline=='@searchTearm'");
    

    And I would like to filter the nodes like this

    nodes.Where("contentHeadline LIKE '%@searchTearm%'");
    

    Can someone help me, please? How can I achieve this with razor syntax?

    Thanks and regards,

    Nadine

    UPDATE:

    I tried to use a Lambda-Syntax like this

            nodes= nodes.Where(x => x.getProperty<String>("contentHeadline").Contains(searchTerm));
    

    but I cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

  • Michaël Vanbrabandt 863 posts 3343 karma points c-trib
    Mar 21, 2018 @ 08:35
    Michaël Vanbrabandt
    0

    Hi Nadine,

    can you post the code where you assign the property nodes? If you are using dynamics then I would suggest moving to strongly types then you can use:

     nodes= nodes.Where(x => x.getProperty<String>("contentHeadline").Contains(searchTerm));
    

    Like in your update.

    Hope this helps.

    /Michaël

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 08:37
    Alex Skrypnyk
    0

    Hi Nadine

    Let's rewrite all code to the strongly typed, then your code with linq where query will be working.

    /Alex

  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 09:19
    Nadine Fisch
    0

    Dear all,

    thank you for your fast reply.

    I select the nodes like this

    CurrentPage.OverviewPage.First().Children;
    

    How can I convert it to the strongly typed? I am on a UmbracoTemplatePage.

    Thanks

    /Nadine

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 09:30
    Alex Skrypnyk
    0

    Hi Nadine

    What type of property editor is OverviewPage? Is it some type of Node picker?

    /Alex

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 09:32
    Alex Skrypnyk
    0

    If it's multinode tree picker, then the code will look like:

    var nodes = Umbraco.AssignedContentItem.GetPropertyValue<IEnumerable<IPublishedContent>>("OverviewPage");
    
    nodes = nodes.Where(x => x.GetPropertyValue<String>("contentHeadline").Contains(searchTerm));
    
  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 09:38
    Nadine Fisch
    0

    The overviewpage is the first Children of the currentPage with a specific documenttype , it is not a property.

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 09:41
    Alex Skrypnyk
    100

    Then this code:

    var nodes = Umbraco.AssignedContentItem.Children.First().Children;
    
    nodes = nodes.Where(x => x.GetPropertyValue<String>("contentHeadline").Contains(searchTerm));
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 09:41
    Alex Skrypnyk
    0

    So you need child nodes of first child?

  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 09:50
    Nadine Fisch
    0

    Yes, exactly.

    But this needs to be more specific

    var nodes = Umbraco.AssignedContentItem.Children.First().Children;
    

    This selections " Umbraco.AssignedContentItem.Children" must relates to the DocumentType "Fragen"

    At the Moment my selector for the overviewpage is

    CurrentPage.Fragen;
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 09:51
    Alex Skrypnyk
    0
    var nodes = Umbraco.AssignedContentItem.Children.Where(x => x.DocumentTypeAlias.Equals("Fragen")).First().Children;
    

    This is more specific - first child and docTypeAlias equals "Fragen"

  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 10:03
    Nadine Fisch
    0

    Ok, I think we are on the right track. Thank you very much!! But first, I have to fix the subsequent faults. I cant access the method "Count()" for example or the properties directly. I think I get the properties from the method "getPropertyValue" ?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 10:14
    Alex Skrypnyk
    0

    Nadine, you also can use MoldesBuilder for easier access to properties.

    You have to cast the node to ModelsBuilder type, for example you can get typed fragen node like that:

    var fragetNodeTyped = Umbraco.AssignedContentItem.Children.First() as Fragen;
    
  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 10:39
    Nadine Fisch
    0

    For this statement, I get the error: 'System.InvalidOperationException: Sequence contains no elements'

    var nodes = Umbraco.AssignedContentItem.Children.Where(x => x.DocumentTypeAlias.Equals("Fragen")).First().Children;
    

    I looked at the Template for the DocumentTypeAlias , it is "Fragen", so it should be ok.

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 10:44
    Alex Skrypnyk
    1

    Nadine, did you look at a template? DocumentTypeAlias it's in the document type settings.

  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 10:54
    Nadine Fisch
    0

    You are right :) In the Document Type Settings it is lowercase written. Thanks again :)

  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 10:59
    Nadine Fisch
    0

    Just one more Question:

    How can I check the number of elements from this statement

    var nodes = Umbraco.AssignedContentItem.Children.Where(x => x.DocumentTypeAlias.Equals("fragen")).First().Children;
    

    I hope that's it. :)

  • Michaël Vanbrabandt 863 posts 3343 karma points c-trib
    Mar 21, 2018 @ 11:14
    Michaël Vanbrabandt
    2

    Hi Nadine,

    you can use nodes.Count() to get the number of elements returned by your query.

    Hope this helps.

    /Michaël

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 11:17
    Alex Skrypnyk
    1

    Hi Nadine,

    Just use .Count() method, like that:

    var amount = nodes.Count();
    
  • Nadine Fisch 159 posts 428 karma points
    Mar 21, 2018 @ 11:31
    Nadine Fisch
    2

    Sorry and thank you, guys. It seems not to work, because I overwrote an existing variable with a dynamic data type.

    But now I get the correct result. AWESOME :) You saved my day! Thanks!

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Mar 21, 2018 @ 11:35
    Alex Skrypnyk
    0

    You are welcome, Nadine, have a great day too.

    It's nice to be involved in process rewriting dynamic types code to strongly typed :))

  • 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