VB.NET中PHP array_count_values的等价物

时间:2015-11-18 05:16:25

标签: vb.net

我是VB.Net的新手,因为我来自PHP Development。我想问一下VB.NET中是否有像array_count_values这样的函数。

我需要计算excel上所有出现的项目。

3 个答案:

答案 0 :(得分:0)

您可能正在寻找数组对象的Array.Length Property

  

获取数组所有维度中元素的总数。

来自MSDN本身的

示例

using System;

public class Example
{
    public static void Main()
    {
        // Declare a single-dimensional string array
        String[] array1d = { "zero", "one", "two", "three" };
        ShowArrayInfo(array1d);
    }

    private static void ShowArrayInfo(Array arr)
    {
        Console.WriteLine("Length of Array:      {0,3}", arr.Length);
        Console.WriteLine("Number of Dimensions: {0,3}", arr.Rank)
        Console.WriteLine();
    }
}

<强>输出

// The example displays the following output:
//       Length of Array:        4
//       Number of Dimensions:   1

答案 1 :(得分:0)

试试这个:

Dim brands() As String = New String() {"HP", "IBM", "HP", "HP", "MICROSOFT"}
        Dim result = From brand In brands
                     Group brand By brand Into brandgroup = Group

        For Each e In result
            Console.WriteLine("brand : {0} count : {1}", e.brand, e.brandgroup.Count)
        Next

答案 2 :(得分:0)

使用LINQ

 Dim myarray As String() = {"HP", "IBM", "HP", "HP", "MICROSOFT"} 

 Dim occur = myarray.GroupBy(Function(f) f).Select( _
                                        Function(f1) New With {.Item = f1.Key, .Count = f1.Count()})

Dim out As String
For Each itm In occur
    out = out & itm.ToString & vbCrLf
Next
MsgBox(out)
{Item = MICROSOFT, Count = 1}
{Item = HP, Count = 3}
{Item = IBM, Count = 1}