代码之家  ›  专栏  ›  技术社区  ›  Soham Dasgupta

要列出的IEnumerable集合

  •  4
  • Soham Dasgupta  · 技术社区  · 14 年前

    我编写了一个CustomerCollection类,它实现了IEnumerable和IEnumerator接口。现在,我希望customerCollection类对象可以通过where()和find()函数进行搜索,还希望从customerCollection类中获取customer类型的列表对象。请帮忙。另外,接口的实现是否正确。

    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 回复  |  直到 14 年前
        1
  •  1
  •   Daniel Dyson    14 年前

    这会满足你的需要。注意,我抽象了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();
        }
    }
    
        2
  •  4
  •   Matt Greer    14 年前

    你可以打电话 Cast<Customer>() 在你的IEnumerable上,它会给你一个 IEnumerable<Customer> 或者只是执行 IEnumerable<客户> 首先。Linq几乎完全被 IEnumerable<T> 不是 IEnumerable . 一旦你做到了这一点,你就可以免费得到所有的物品。

        3
  •  2
  •   abatishchev Karl Johan    14 年前

    我建议使用 OfType<T>() 而不是 Cast<T>() 因为如果你的收藏包含T1和T2, collection.Cast<T1>() 将引发错误 collection.OfType<T1>() 将返回 IEnumerable<T1> 仅包含T1而不是T2的实例

        4
  •  1
  •   chugh97    14 年前
    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();  
        }    
    
    }  
    
        5
  •  1
  •   manixrock    14 年前
    new List<Customer>(myCustomerEnumerator);
    
        6
  •  1
  •   Cameron MacFarland    14 年前

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

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

    public class CustomerCollection : Collection<Customer>
    {
    }