First of all I would like to mention what @Tim Schmelter said -
Have you noticed already that the real problem is your creepy
datamodel? Use a table with real records instead of a column with a
comma separated string.
It is not a good practice to use a datamodel
where you need string split match. Because it leads to inefficient systems and not to mention slow queries. But yet, if you really need a solution why not try this -.
There are four occasions where you will get a match,
- A prefix match - starting with
- Inner Match - contains with
- Suffix Match - ends with
- The only match - only one item and this is it
considering the scenario I am suggesting the solution below -
s is the value looking for say "1"
string prefixMatch = s + ",";
string suffixMatch = "," + s;
string innerMatch = "," + s + ",";
string record = <dbRecords>.FirstOrDefault(r=> r.StartsWith(prefixMatch) ||
r.Contains(innerMatch) || r.EndsWith(suffixMatch) ||
(!r.Contains(",") && r == s));
The reason for such a detailed query is to keep your memory utilisation less and letting the SQL query do the hard work of finding the results because this query will support LINQ-to-SQL conversion.