Marc Marc - 1 year ago 188
C# Question

string[*,*] does not contain a definition for 'Contains'

I have a function that checks if a 2-dimensional string array contains a specific string value using .Contains. System.Linq is being used, as seems to be the problem in similar questions, however I still get the error of: 'string[,]' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains(IQueryable,string)' requires a receiver of type 'IQueryable'.

This error persists no matter what I change the comparison value to. The context for the error occuremce is as

string comparisonString = " ";

bool victoryRequirement = mineArray.Contains(comparisonString);

I hope someone can tell me why this error occurs and whether or not I am able to use Contains for this purpose. I suspect the 2-dimensional array is partly at fault, but I am not that experienced.

Answer Source

You can write your own contains method for two dimensional string array like below:

public bool TwoDimensionalContains(
    string[,] inputArray, 
    string comparisonString)
    for (int i = 0; i < inputArray.GetLength(0); i++)
        for (int j = 0; j < inputArray.GetLength(1); j++)
            // If matching element found, return true
            if (comparisonString.Equals(inputArray[i, j]))
                return true;
    // No matchincg element found, return false
    return false;

Then use it as below:

string[,] myArray = new string[2, 2]
    { "One", "Two" },
    {  "Three", "Four" }

bool contains = TwoDimensionalContains(myArray, "Three");

You can also make TwoDimensionalContains method, extension method to use it as other Linq methods. For more information, look at this example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download