【问题标题】:System.Array does not contain a definition for 'Any' - C#System.Array 不包含“任何”的定义 - C#
【发布时间】:2015-07-24 14:47:29
【问题描述】:

使用 Visual Basic // C#

我正在尝试在我存储的数组中搜索与用户输入匹配的内容。 例如,用户存储了 USB 的数据,现在希望恢复该信息。

完整代码如下

我已经在使用 IndexOf 来查找数组索引,但现在我想搜索该索引以匹配用户的输入。 这行代码:

if (ProductNameArray.Any(usersearch.Contains))

出现了错误

System.Array 不包含“任何”的定义

但它在其他代码中对我有用。

我似乎无法弄清楚这一点,感谢任何帮助。

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Prac_Test3
{


class Program
{
     // Constants
        const int SIZE_OF_PRODUCT_CODE = 4;
        const float CATEGORY_A_MARKUP = 10.0F;
        const float CATEGORY_C_MARKUP = 33.3F;
        const float CATEGORY_P_MARKUP = 15.0F;
        const int ARRAY_SIZE = 100;


    static void DisplayMenu()
    {
        Console.Clear();
        Console.WriteLine("--------------> Menu <------------");
        Console.WriteLine("1. Add a product                (a)");
        Console.WriteLine("2. Find a product               (f)");
        Console.WriteLine("3. Enter the quantity in stock  (q)");
        Console.WriteLine("4. Delete a product             (d)");
        Console.WriteLine("5. Calculate and display values (v)");
        Console.WriteLine("6. Exit                         (x)");
        Console.Write("\r\nEnter your selection: ");
    }
    static void AddProduct( string[] ProductNameArray, string[] ProductCodeArray, float[] WholesalePriceArray, ref int NextAvaliablePosition)
    {
        string ProductName = "";
        string ProductCode = "";
        string ProductCategory = "";

        float WholesalePricePerItem = 0.0F;

        bool ParseResult = false;
        bool ErrorFlag = false;

        string UserResponse = ""; 

        do
        {
            ErrorFlag = false;
            Console.Write("Product Name                : ");
            ProductName = Console.ReadLine();
            if (ProductName == "")
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Product Name must not be left blank.");
                Console.ForegroundColor = ConsoleColor.Gray;
                ErrorFlag = true;
            }
        } while (ErrorFlag);

        do
        {
            ErrorFlag = false;
            Console.Write("Product Code                : ");
            ProductCode = Console.ReadLine();
            if (ProductCode == "")
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Product Name must not be left blank.");
                Console.ForegroundColor = ConsoleColor.Gray;
                ErrorFlag = true;
            }
            else if (ProductCode.Length != SIZE_OF_PRODUCT_CODE)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Product Code must be exactly four characters.");
                Console.ForegroundColor = ConsoleColor.Gray;
                ErrorFlag = true;
            }
            else
            {
                ProductCategory = ProductCode.Substring(0, 1);
                ProductCategory = ProductCategory.ToUpper();
                if (ProductCategory != "A" && ProductCategory != "C" && ProductCategory != "P")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Product Code must start with A, C or P.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    ErrorFlag = true;
                }
                else if (!(Char.IsDigit(ProductCode[1])) && !(Char.IsDigit(ProductCode[2])) && !(Char.IsDigit(ProductCode[3])))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Product Code  must be A, C or P followed by three digits.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    ErrorFlag = true;
                }
            }
        } while (ErrorFlag);

        do
        {
            ErrorFlag = false;
            Console.Write("Wholesale price per item ($): ");
            UserResponse = Console.ReadLine();
            ParseResult = float.TryParse(UserResponse, out WholesalePricePerItem);
            if (ParseResult == false)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Not a valid number.");
                Console.ForegroundColor = ConsoleColor.Gray;
                ErrorFlag = true;
            }
            else if (WholesalePricePerItem <= 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Wholesale price must be a number greater than 0.");
                Console.ForegroundColor = ConsoleColor.Gray;
                ErrorFlag = true;
            }
        } while (ErrorFlag);
    }
    static void FindProduct(Array ProductNameArray)
    {

        int search = -1;
        string usersearch;

        usersearch = Console.ReadLine();
        search = Array.IndexOf(ProductNameArray, usersearch);

        if (search >=0)
        {
            if (ProductNameArray.Any(usersearch.Contains))
            {
                Console.WriteLine(" details blah blah");
            }
        }
        else if (search <0)
        {
            Console.WriteLine("No record exists.");
        }    

【问题讨论】:

  • 您确定要添加System.Linq 命名空间吗?您的 .NET Framework 版本是多少?
  • 我不明白你的意思,你能解释一下吗? (我是一年级编程学生)

标签: c# arrays search


【解决方案1】:

你需要 using System.Linq; 才能工作。

Any 是 LINQ 中定义的扩展方法。

另外,注意ProductNameArray的类型。如果它被定义为Array(而不是string[],例如),编译器无法推断,当枚举时,它将产生strings。

在这种情况下,你必须写:

if (ProductNameArray.Cast<string>().Any(usersearch.Contains))

编辑:好的,看代码似乎是上面描述的问题。

您必须从

更改 FindProduct 方法的签名
static void FindProduct(Array ProductNameArray)

static void FindProduct(string[] ProductNameArray)

或使用.Cast&lt;string&gt; 方法。

我个人更喜欢更改方法的签名,因为传递给它的 ProductNameArray 似乎真的是 string[]

【讨论】:

  • 我在代码的最顶部使用 System;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用 System.Threading.Tasks;你是这个意思吗?
  • @Shandep 是的,我就是这个意思。
  • @Shandep 也许在问题中包含所有代码是个好主意。
  • 这部分在一个方法中,我不认为其余部分是相关的。你建议我在我的问题中添加哪些部分?
  • @Shandep 我不知道代码,所以我不知道它的哪些部分是相关的。理想情况下,您会发布所有代码,例如,查看在当前范围内没有隐藏 System.Array 类的自定义 Array 类。
【解决方案2】:

Any()System.Linq 命名空间中的扩展方法。您必须添加using System.Linq; 才能使用它。

namespace Your.Namespace
{
    using System;
    using ... // your other usings
    using System.Linq;

    public sealed class YourClass
    {
        public void Test()
        {
            // ...
            yourArray.Any()
        }
    }
}

【讨论】:

  • 我在我的代码的最顶部有它以及其他东西。
【解决方案3】:

添加下面的代码

using System.Linq;

【讨论】:

    猜你喜欢
    • 2013-11-15
    • 2014-11-23
    • 2015-02-05
    • 2012-09-29
    • 1970-01-01
    • 2011-06-21
    • 2021-08-16
    • 2013-03-26
    • 2020-04-27
    相关资源
    最近更新 更多