IEnumerable集合到List

时间:2010-08-24 14:05:50

标签: c# .net

我编写了一个CustomerCollection类,它实现了IEnumerable和IEnumerator接口。现在我希望CustomerCollection类对象可以通过Where()和Find()函数进行搜索,并且还希望从CustomerCollection类中获取Customer类型的List对象。请帮忙。此外,接口的实现是正确的。

public class Customer
{
    private int _CustomerID;
    private string _CustomerName;

    public Customer(int customerID)
    {
        this._CustomerID = customerID;
    }

    public int CustomerID
    {
        get
        {
            return _CustomerID;
        }
        set
        {
            _CustomerID = value;
        }
    }

    public string CustomerName
    {
        get
        {
            return _CustomerName;
        }
        set
        {
            _CustomerName = value;
        }
    }
}

public class CustomerController
{
    public ArrayList PopulateCustomer()
    {
        ArrayList Temp = new ArrayList();

        Customer _Customer1 = new Customer(1);
        Customer _Customer2 = new Customer(2);

        _Customer1.CustomerName = "Soham Dasgupta";
        _Customer2.CustomerName = "Bappa Sarkar";

        Temp.Add(_Customer1);
        Temp.Add(_Customer2);

        return Temp;
    }
}

public class CustomerCollection : IEnumerable, IEnumerator
{
    ArrayList Customers = null;
    IEnumerator CustomerEnum = null;

    public CustomerCollection()
    {
        this.Customers = new CustomerController().PopulateCustomer();
        this.CustomerEnum = Customers.GetEnumerator();
    }

    public void SortByName()
    {
        this.Reset();
    }

    public void SortByID()
    {
        this.Reset();
    }

    public IEnumerator GetEnumerator()
    {
        return (IEnumerator)this;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return (IEnumerator)this;
    }

    public void Reset()
    {
        CustomerEnum.Reset();
    }

    public bool MoveNext()
    {
        return CustomerEnum.MoveNext();
    }

    public object Current
    {
        get
        {
            return (Customer)CustomerEnum.Current;
        }
    }

}

6 个答案:

答案 0 :(得分:4)

您可以在IEnumerable上调用Cast<Customer>(),这将为您提供IEnumerable<Customer>,或者只是开始实施IEnumerable<Customer>。 LINQ几乎完全挂钩IEnumerable<T>,而不是IEnumerable。一旦你这样做,你就可以免费获得所有LINQ to objects goodness。

答案 1 :(得分:2)

我建议使用OfType<T>()代替Cast<T>(),因为如果您的收藏包含T1和T2,collection.Cast<T1>()会在collection.OfType<T1>()返回IEnumerable<T1>时抛出错误仅包含T1而非T2的实例

答案 2 :(得分:1)

这将做你想要的。注意,我已经抽象出IEnumerable以使其可重用并降低了所有其他类的复杂性。

//Write your Test first
public class Test
{
    public void TestEnumerator()
    {
        var customers = new CustomerCollection();
        var qry = 
            from c in customers
            select c;

        foreach (var c in qry)
        {
            Console.WriteLine(c.CustomerName);
        }

        //Create a new list from the collection:
        var customerList = new List<Customer>(customers);
    }
}

public abstract class MyColl<T> : IEnumerable<T>
{
    protected T[] Items;
    public IEnumerator<T> GetEnumerator()
    {
        foreach (T item in Items)
        {
            yield return item;
        }
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class Customer
{
    public Customer(int customerID)
    {
        CustomerID = customerID;
    }
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
}

public class CustomerController
{
    public Customer[] PopulateCustomer() {
       return new [] {new Customer(1) {CustomerName = "Soham Dasgupta"},
                      new Customer(2) {CustomerName = "Bappa Sarkar"}};
    }
}

public class CustomerCollection : MyColl<Customer>
{

    public CustomerCollection()
    {
        Items = new CustomerController().PopulateCustomer();
    }
}

答案 3 :(得分:1)

public class CustomerController 
{ 
    public List<Customer> PopulateCustomer() 
    { 
        List<Customer> Temp = new ArrayList(); 

        Customer _Customer1 = new Customer(1); 
        Customer _Customer2 = new Customer(2); 

        _Customer1.CustomerName = "Soham Dasgupta"; 
        _Customer2.CustomerName = "Bappa Sarkar"; 

        Temp.Add(_Customer1); 
        Temp.Add(_Customer2); 

        return Temp; 
    } 
} 


public class CustomerCollection : List<Customer>
{  
    List<Customer> Customers = new List<Customer>();  


    public CustomerCollection()  
    {  
        this.Customers = new CustomerController().PopulateCustomer();  
    }    

}  

答案 4 :(得分:1)

new List<Customer>(myCustomerEnumerator);

答案 5 :(得分:1)

用于创建自己的集合实现的推荐基类是System.Collections.ObjectModel.Collection<T>

  

(来自MSDN)
  提供此基类是为了使实现者更容易创建自定义集合。鼓励实施者扩展此基类,而不是创建自己的基类。

public class CustomerCollection : Collection<Customer>
{
}