No Overload for method 'Contains' takes 2 arguments with list and simple string
Asked Answered
L

3

6

Borrowing from the example here, I tried to do the following:

List<string> animals = new List<string> { "Horse", "Cat", "Dog" };
bool testCase = animals.Contains("horse", StringComparer.CurrentCultureIgnoreCase);

But just doing that, I get "No overload for method 'Contains' takes 2 arguments".

I also tried it as:

List<string> animals = new List<string> { "Horse", "Cat", "Dog" };
string testAnimal = "horse";
bool testCase = animals.Contains(testAnimal, StringComparer.CurrentCultureIgnoreCase);
testCase = animals.Contains((string)testAnimal, StringComparer.CurrentCultureIgnoreCase);

But both of those get the same error.

what am I missing here?

Loblolly answered 2/3, 2020 at 22:12 Comment(2)
Does this answer your question? Searching a List<string> for an EXACT case insenitive matchGilgamesh
No. The problem was that I didn't have system.linq, and that page doesn't mention it at all.Loblolly
S
0

You are probably searching for the Linq-extension method Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>) which is documented here.

In your case, it is probably sufficient to just add using System.Linq; to the top of your source file and the method pops up.

Scutari answered 2/3, 2020 at 22:17 Comment(3)
My system doesn't have the linq namespace... and I'm following the steps athttps://mcmap.net/q/512517/-the-type-or-namespace-name-39-linq-39-does-not-exist-in-the-namespace-39-system-39-duplicate and I don't even see system.core or system.data.linqLoblolly
there we go... somehow my target framework was set to 2.0 ... yeesh...Loblolly
Unless you are on a really old version of .net Framework (< 3.5(, you must have System.Linq available.Arevalo
L
1

Note that the overloaded method is only available on target Win 10 version 1809 and higher.

Lowrey answered 8/11, 2021 at 9:27 Comment(0)
S
0

You are probably searching for the Linq-extension method Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>) which is documented here.

In your case, it is probably sufficient to just add using System.Linq; to the top of your source file and the method pops up.

Scutari answered 2/3, 2020 at 22:17 Comment(3)
My system doesn't have the linq namespace... and I'm following the steps athttps://mcmap.net/q/512517/-the-type-or-namespace-name-39-linq-39-does-not-exist-in-the-namespace-39-system-39-duplicate and I don't even see system.core or system.data.linqLoblolly
there we go... somehow my target framework was set to 2.0 ... yeesh...Loblolly
Unless you are on a really old version of .net Framework (< 3.5(, you must have System.Linq available.Arevalo
B
0

you have to use the extension method Contains that is part of the namespace System.Linq. Just add this line on top of your file and the method with two arguments should be available.

using System.Linq; 
Backwoodsman answered 2/3, 2020 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.