System.Array does not contain a definition for 'Any' - C#

后端 未结 3 1220
旧巷少年郎
旧巷少年郎 2021-01-24 04:14

Using Visual Basic // C#

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 wish

相关标签:
3条回答
  • 2021-01-24 04:24

    You need using System.Linq; for that to work.

    Any is an extension method defined in LINQ.

    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))
    

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

    You'll have to change the signature of the FindProduct method from

    static void FindProduct(Array ProductNameArray)
    

    to

    static void FindProduct(string[] ProductNameArray)
    

    or use the .Cast<string> method.

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

    0 讨论(0)
  • 2021-01-24 04:29

    Any() is an extension method in System.Linq namespace. You have to add using System.Linq; so that you can use it.

    namespace Your.Namespace
    {
        using System;
        using ... // your other usings
        using System.Linq;
    
        public sealed class YourClass
        {
            public void Test()
            {
                // ...
                yourArray.Any()
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-24 04:30

    Add below to you code

    using System.Linq;
    
    0 讨论(0)
提交回复
热议问题