Is it possible to do a LINQ where clause and split a varchar field on a : into a collection and compare this to another collection to see if any values match.
For example, we have a List<string> called AllowedHolders which contains ARR ACC etc.. however the field in the database (which unfortunately we cannot change) is a varchar but has values separated by a colon: i.e. “:ARR:ACC:”
What we would like to do is write a LINQ where clause which can check if any of the AllowedHolders appear in the field. As we wish to restrict the results to only bring back records where the field contains a value in the AllowedHolders collection.
We have done the following where the field only contains a single value
searchResults = searchResults.Where(S => searchParams.AllowedBusinessAreas.Contains(S.SIT_BusinessArea));
But the following will not work because SIT_HolderNames contains values separated by a colon:
searchResults = searchResults.Where(S => searchParams.AllowedHolders.Contains(S.SIT_HolderName)
Any ideas would be much appreciated. If you need me to explain anything further please let me know.
Andy
Maybe you can use:
or
As pointed out by the first comment, this only works if no holder name contains another holder name as a substring. I was implicitly assuming that all your holder names were three-letter strings like
ARRandACC. If this is not the case, consider using(":" + H + ":"), or find a more safe solution.Edit: Just for completeness, here are two versions with colons prepended and appended:
And: