Copied to clipboard

Flag this post as spam?

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


  • Edvin 9 posts 99 karma points
    Feb 14, 2019 @ 17:40
    Edvin
    0

    Filtering Repeatable String Property in Linq

    Hi,

    so I am having a custom search form, basically the user types in a keyword and I search the collection of Persons based on his properties and if they contain the keyword.

    So user types in "Berlin" and I filter all persons which contain the keyword in there property:

            var filteredMedia = Model.Persons.Where(x => x.GetPropertyValue<string>("city").ToUpper().Contains(searchTerm.ToUpper())).Reverse();
    

    So this works great with normal text string fields, but if I use the repeatable string, then I have a problem. I can't use the GetPropertyValue

    I am currently fixed this by doing it the "manual" way, getting the collection and filter out the ones through two for each loops, but I am afraid that could be a bit slow.

    Any Ideas how I could filter out the repeatable strings directly throught Linq?

    Thx!

  • Matthew Wise 271 posts 1373 karma points MVP 5x c-trib
    Feb 18, 2019 @ 07:36
    Matthew Wise
    0

    Hi Edvin,

    Repeatable text string should return an array so updating your where clause to -

    x.GetPropertyValue<string[]>("city").Contains(searchTerm.ToUpper(),StringComparer.InvariantCultureIgnoreCase)
    

    I would also recommend null checking x in case someone unpublished a Person that is selected.

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft