0

I have the following code:

string s = "123,12346";

bool b = s.Contains("1234");

The above returns true, but is there a way to return false. I have already done something like:

string[] s = {"123","12346"};

bool b = s.Contians("1234");

The above works, but I can't use the above in a contains expression with LINQ-To-Entities because it does not like Contains in pre EF 4.0.

I have an extension method which behaves like a SQL IN clause, but I need to explicitly type the parameters when I use it, I am not sure what to put between the brackets:

predicate = predicate(x=> WhereIn<>(x.Id, Ids));

x.Id is a string and Ids is a string as well. If I put WhereIn<string,string>, it complains that Argument type string is not assignable to parameter type ObjectQuery<string>

predicate comes from the PredicateBuilder: http://www.albahari.com/nutshell/predicatebuilder.aspx

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • The database schema obviously needs fixing. "x,y,z" in one column is wrong. – Jon May 30 '13 at 14:24
  • 2
    You don't want string.Contains to return false. You want to give a more accurate description of what you actually hope to achieve. string.Contains returning false isn't it. – Anthony Pegram May 30 '13 at 14:25
  • 1
    I didn't vote down, but maybe you should clarify why "123,12346" should return false. Should it only return true in case of "123,1234" for example? – John Willemse May 30 '13 at 14:25
  • @JohnWillemse - I know that it is supposed to return true, the only problem I have is that once I convert it to an array, I can't use it with LINQ-To-Entities. – Xaisoft May 30 '13 at 14:26
  • 1
    think you are looking for this http://stackoverflow.com/questions/374267/contains-workaround-using-linq-to-entities – Hector Sanchez May 30 '13 at 14:28
  • @Mr. - That is the extension method I am using, but when I do predicate = predicate.And(x=> WhereIn(x.Id,Ids), it wants me to specify TEntity and TValue explicity between in WhereIn<>, I am just not sure what. – Xaisoft May 30 '13 at 14:31
  • http://stackoverflow.com/a/374703/728314 like this one? – Hector Sanchez May 30 '13 at 18:20
  • How about this one? `",123,12346,".Contains(",1234,")` – Vitaliy Feb 10 '23 at 19:30

1 Answers1

-2

Use

string searchString = "123,12346";
bool b = sep(searchString,",").Contains("1234");

//Returns this string before ","
public static string sep(string s,string delimiter)
{
   int indx = s.IndexOf(delimiter);
   if (indx >0)
   {
      return s.Substring(0, indx);
   }
   return "";
}
Ikado
  • 11
  • 3
  • That's going to check only the first item in a list of unknown length. This does _not_ solve the question. – DonBoitnott May 30 '13 at 14:42
  • 1
    I can't tell how this is supposed to answer the question - you throw away all of the string after the first comma, or return an empty string if there's no comma. This would still return `true` for `"123456,123"` (which I believe the OP would not want), and would return false for `"1234"` which also seems wrong. And it looks as though you're trying (and failing) to re-implement `string.Split`. Can you explain what this is supposed to be? – Dan Puzey May 30 '13 at 14:43
  • You are right, need iteration to catch all delimiters. – Ikado Jun 17 '13 at 06:40