如何确定数组中唯一派生类型的数量?

时间:2016-06-26 13:18:00

标签: c# .net arrays linq

我有以下课程

Customer班级

abstract class Customer
{
    public int id;
    public string name;
    public double balance;
}

班级NormalCustomer

class NormalCustomer: Customer
{
}

班级SubscriberCustomer

class SubscriberCustomer:Customer
{
    public int LandMinutes;
    public int MobileMinutes;
}

如果我们创建一个Customers

数组
Customer[] customers = new Customer[100];
customers[0]=new NormalCustomer();
customers[1] = new NormalCustomer();
customers[2] = new SubscriberCustomer();
customers[3] = new NormalCustomer();
customers[4] = new SubscriberCustomer(); 

问题是如何知道数组中有多少个对象NormalCustomers以及数组中有多少个对象是SubscriberCustomers

4 个答案:

答案 0 :(得分:8)

您可以使用OfType扩展方法

customers.OfType<NormalCustomer>().Count() 

您需要使用using指令导入System.Linq命名空间:

using System.Linq;

答案 1 :(得分:3)

我同意Aleksey的回答 - 但如果你想要其他选择:

// Result is a sequence of Type/Count objects
var groupedByActualType = customers.GroupBy(
    x => x.GetType(), (type, values) => new { Type = type, Count = values.Count() });

或者:

var normal = customers.Count(c => c is NormalCustomer);
var subscribers = customers.Count(c => c is SubscriberCustomer);

答案 2 :(得分:0)

您可以使用LINQ并将它们分组:

var groupedCustomers = customers.GroupBy(c => c.GetType()).ToList();

foreach(var customer in groupedCustomers)
{
   Console.WriteLine(customer.Key.Name + ", count: " + customer.Count());
}

https://ideone.com/fz93H6

确保您已包含System.Linq命名空间:

using System.Linq;

答案 3 :(得分:0)

这是一种方法

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer[] customers = new Customer[100];
            customers[0] = new NormalCustomer();
            customers[1] = new NormalCustomer();
            customers[2] = new SubscriberCustomer();
            customers[3] = new NormalCustomer();
            customers[4] = new SubscriberCustomer();

            int normalCount = 0;
            int subscriberCount = 0;

            foreach (Customer customer in customers)
            {
                if (customer != null)
                {
                    string customerName = customer.GetType().ToString();
                    // get name without namespace
                    customerName = customerName.Substring(customerName.LastIndexOf(".") + 1);
                    switch (customerName)
                    {
                        case "NormalCustomer":
                            normalCount++;
                            break;
                        case "SubscriberCustomer":
                            subscriberCount++;
                            break;
                    }
                }

            }
        }
    }
    abstract class Customer
    {
        public int id;
        public string name;
        public double balance;
    }
    class NormalCustomer : Customer
    {
    }
    class SubscriberCustomer : Customer
    {
        public int LandMinutes;
        public int MobileMinutes;
    }
}