System.Array不包含“任何"的定义-C# [英] System.Array does not contain a definition for 'Any' - C#

查看:123
本文介绍了System.Array不包含“任何"的定义-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual Basic//C#

Using Visual Basic // C#

我正在尝试通过存储的数组搜索与用户输入匹配的内容. 例如,用户已经为USB存储了数据,现在希望备份该信息.

I'm trying to search through my stored arrays for a match to the user input. For example, the user has stored the data for a USB, and now wishes to bring that information back up.

下面的完整代码

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

I'm already using IndexOf to find the array index, but now i want to search that index for a match to the user's input. This line of code:

if (ProductNameArray.Any(usersearch.Contains))

提出了

System.Array不包含任何"的定义

System.Array does not contain a definition for 'Any'

但是它对我来说已经在其他代码中起作用了.

but it has worked in other code for me.

我似乎无法解决这个问题,感谢您的帮助.

I cannot seem to figure this out, any help is appreciated.

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.");
        }    

推荐答案

您需要using System.Linq;才能正常工作.

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

Any is an extension method defined in LINQ.

此外,请注意ProductNameArray的类型.如果将其定义为Array(而不是例如string[]),则编译器无法推断出枚举时将产生string s的方法.

Also, pay attention to the type of ProductNameArray. If it is defined as Array (and not string[], for example), the compiler has no way of inferring that, when enumerated, it'll yield strings.

在这种情况下,您必须编写:

In that case, you'd have to write:

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


好吧,看一下代码,似乎问题是上述问题.


OK, looking at the code it seems that the problem is the one described above.

您必须从

static void FindProduct(Array ProductNameArray)

static void FindProduct(string[] ProductNameArray)

或使用.Cast<string>方法.

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

I'd personally prefer changing the method's signature, since the ProductNameArray passed to it seems to really be a string[].

这篇关于System.Array不包含“任何"的定义-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆