简体   繁体   English

字符串[*,*]不包含“包含”的定义

[英]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 . 我有一个函数,使用.Contains检查二维字符串数组是否包含特定的字符串值。 System.Linq is being used, as seems to be the problem in similar questions, however I still get the error of: 正在使用System.Linq ,似乎是类似问题中的问题,但是我仍然收到以下错误:

'string[ , ]' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains(IQueryable,string)' requires a receiver of type 'IQueryable'. 'string [ ]'不包含'Contains'的定义,最佳扩展方法重载'Queryable.Contains(IQueryable,string)'需要类型为'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. 我希望有人可以告诉我为什么会发生此错误,以及我是否能够为此目的使用“ Contains ”。 I suspect the 2-dimensional array is partly at fault, but I am not that experienced. 我怀疑二维数组部分出错,但是我没有那么经验。

Though the answer of Adil Mammodov appears to work, it is not very general. 尽管Adil Mammodov的答案似乎很奏效,但这不是很笼统。 I would have preferred to write this as the much shorter and more flexible: 我更愿意将其编写为更短,更灵活的代码:

public static IEnumerable<T> ToSequence<T>(this T[,] items)
{
  return items.Cast<T>();
}

And now you can simply use ToSequence to turn an array into a sequence, and then apply a sequence operator to it. 现在,您可以简单地使用ToSequence将数组转换为序列,然后对其应用序列运算符。

myItems.ToSequence().Contains(target);

Also, while we're looking at your code: try to name things according to what they do, not what type they are. 另外,当我们在查看您的代码时:尝试根据事物的用途而不是其类型来命名事物。 If you have "Array" and "String" in names of things, they could have better, more descriptive names. 如果事物名称中包含“ Array”和“ String”,则它们可能具有更好的,更具描述性的名称。 The type is already annotated in the type. 该类型已经在类型中进行了注释。

You can write your own contains method for two dimensional string array like below: 您可以为二维字符串数组编写自己的contains方法,如下所示:

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. 您还可以使TwoDimensionalContains方法, 扩展方法用作其他Linq方法。

Actually this works: 实际上这有效:

var c=myArray.Cast<string>().Select(x=>x).Contains("Three");

even easier: 甚至更容易:

var c=myArray.Cast<string>().Contains("Three");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM