4

I'm trying to populate all the results that contains the letters provided by the user.

foreach (var item in entries.PhonebookList.Where(ab => ab.FirstName.Contains(fName, StringComparison.InvariantCultureIgnoreCase)))
{
    Console.WriteLine("Record: {0}  {1}  {2} ", item.ItemNumber, item.FirstName, item.LastName);
}

But I'm getting an error inside the Contains method.

"No overload for method 'Contains' takes 2 arguments"

What is the best way to use Contains with StringComparison.InvariantCultureIgnoreCase method?

Thank you.

3
  • What is the type of fName - string?
    – Ivan Stoev
    Aug 13, 2017 at 12:01
  • Are you sure that you want Contains but Equals? Aug 13, 2017 at 12:02
  • Yes I need Contains. For example I have Sheng and Sheldon as firstnames. When I use Equals and I provide "she" as parameter for fName. it will not display Sheng and Sheldon since it did not meet the required value. Aug 13, 2017 at 12:16

2 Answers 2

2

According to the documentation here you should use string.IndexOf(string, StringComparison) instead.

So basically its:

    var entries = new 
    {
      PhonebookList = new[]
      {
        new { ItemNumber = 1, FirstName = "Leonard", LastName = "Hofstadter" }, 
        new { ItemNumber = 2, FirstName = "Sheldon", LastName = "Cooper" }, 
        new { ItemNumber = 3, FirstName = "Howard", LastName = "Wolowitz" }, 
        new { ItemNumber = 4, FirstName = "Sheng", LastName = "?" }, 
      }
    };
    
    var fName = "she";
    foreach (var item in entries.PhonebookList.Where(ab => ab.FirstName.IndexOf(fName, StringComparison.InvariantCultureIgnoreCase) >= 0))
    {
        Console.WriteLine("Record: {0} {1} {2}", item.ItemNumber, item.FirstName, item.LastName);
    }

Which returns:

Record: 2 Sheldon Cooper

Record: 4 Sheng ?

If you often need a Contains with a different StringComparison the documentation also provides an extension method you can implement, to use it like Contains (see the given example in the provided link).

8
  • I didn't get any error. I got the same result. if I use "she" as parameter for fName variable. It didnt display the expected result. I have Sheng and Sheldon in my for firstname. Aug 13, 2017 at 12:17
  • See my updated example. I suppose you should double-check if your FirstName fields really contain the names you think they contain.
    – ckuri
    Aug 13, 2017 at 12:33
  • Yup I did. This is what I've observed. I have the following firstnames Breth Dioren Klarence Rey If the parameter of my fName is "re". These names will display. However if i use "RE". They wont be displayed. Aug 13, 2017 at 12:34
  • I checked it with your new examples. And it works for me regardless of the search terms casing. What is the type of PhonebookList? Is it from an EntityFramework / NHibernate /ORM mapper query, i.e. of type IQueryable<...>? If yes, you may need to alter your DB table schema (see stackoverflow.com/questions/14400856/…).
    – ckuri
    Aug 13, 2017 at 12:39
  • public List<PhonebookItem> PhonebookList { get; set; } Aug 13, 2017 at 12:47
0

You should use String.IndexOf method instead of Contains. If you are interested in the position of the sub string value in the current instance, you can call the IndexOf method to get the starting position of its first occurrence, or you can call the LastIndexOf method to get the starting position of its last occurrence. IndexOf(String) method is used if a sub string is found in a string instance. Also you can use other overloads of String.IndexOf based on your purpose like the one with String and StringComparison arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.