Copied to clipboard

Flag this post as spam?

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


  • Tim C 146 posts 502 karma points
    Jun 10, 2016 @ 07:07
    Tim C
    0

    Dynamic Linq query - manipulate properties in 'where' clause?

    I am trying to build some generic reusable code in an Umbraco site, though my question isn't totally Umbraco specific.

    The concept is to build the Linq Query for listing documents on the fly, which means building the where clause in a string. The problem is using Where where both the property being matched and the match value are csv strings.

    I can do this with simple queries without problem eg

    .Where("pageTitle contains(\"news\"))
    

    works fine

    However, I am working on building a system wherby view a document and also automatically list related documents with the same value in a property 'hashTags'. If there were only one hashtag value per document, that would be a simple match, but there could be multiples eg

    Doc A hashtags #currency,#sterling,#gbp Doc B hashtags #gpbconversion,#domestic Doc C hashtags #currency,#gpb

    Let's say I view Doc A. This should then pull in Doc C as a related doc as they both have hashtag #currency

    I could build the where clause by 'Split' the Doc A hashtag to get

    .Where ("(hashTags.Contains(\"#currency\") OR hashTags.Contains(\"#sterling\") OR hashTags.Contains(\"#gbp\"))"
    

    however, this would also return Doc B incorrectly as it has #gpbconversion which of course 'Contains' #gbp as it's a substring.

    In previous systems, I would avoid this by wrapping both the field being searched and the match value in a delimiter to force an exact match

    eg instead of matching Doc B hashtags (value "#gpbconversion,#domestic") against "#gbp" which causes the problem, I would match ",#gpbconversion,#domestic," against ",#gbp," thus ignoring Doc B.

    However, the problem is concatening the delimiter aginst the proberty eg

    (\",\"+hashTags+\",\".Contains(\",#currency,\")...
    

    crashes.

    I have tried concat

    (String.Concat(String.Concat(\",\",hashTags),\",\").Contains(",#currency,")...
    

    but this crashes

    System.ArgumentException: Expression of type 'System.Func`2[Umbraco.Web.Models.DynamicPublishedContent,System.Object]' cannot be used for parameter of type 'System.String' of method 'System.String Concat(System.String, System.String)'
    

    I have tried hashTags.ToString()

    but that still crashes.

    Is there any other way to achieve what I am trying to do....and thanks for reading this far!

  • MarcC 49 posts 351 karma points
    Jun 10, 2016 @ 07:52
    MarcC
    100

    Hey Tim C

    When you say you have a property Hashtags is this just a textstring that you are adding these into, and which to check against?

    If so would it be a possibility to swap this out for the umbraco tag system?

    You can use this package to add tags and then check against the tag objects instead of checking against strings.

    https://our.umbraco.org/projects/backoffice-extensions/tag-manager

    Hope it helps!

  • Tim C 146 posts 502 karma points
    Jun 10, 2016 @ 08:38
    Tim C
    0

    Thanks, Looks very useful. I hand't noticed the Umbraco tag property editor. However, I was using a text string csv so the same problem exists : how to match from one document all other documents that share at least one tag in a where clause

  • Tim C 146 posts 502 karma points
    Jun 10, 2016 @ 08:53
    Tim C
    1

    Found it - Umbraco.TagQuery.GetContentByTag("mytagvalue");

  • MarcC 49 posts 351 karma points
    Jun 10, 2016 @ 08:58
    MarcC
    0

    Ace!

  • Tim C 146 posts 502 karma points
    Jun 10, 2016 @ 10:16
    Tim C
    0

    Except this will only search for one tag at a time; but using Concat got around this. Learning.....

Please Sign in or register to post replies

Write your reply to:

Draft