IEnumerable collection for list

I wrote a CustomerCollection class that implements the IEnumerable and IEnumerator interfaces. Now I want the CustomerCollection class object to be searchable by the Where () And Find () function, and also want to get a List object of type Customer from the CustomerCollection class. Please help. In addition, the correct implementation of the interfaces.

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; } } } 
+4
source share
6 answers

This will do what you want. Notice, I distracted IEnumerable to make it reusable and reduce the complexity of all other classes.

 //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(); } } 
+1
source

You can call Cast<Customer>() on your IEnumerable, which will give you IEnumerable<Customer> or just implement IEnumerable<Customer> for starters. LINQ is almost completely connected to IEnumerable<T> , not IEnumerable . Once you do this, you will get free LINQ support for objects for free.

+4
source

I recommend using OfType<T>() instead of Cast<T>() , because if your collection contains T1 and T2, collection.Cast<T1>() will throw an error, and collection.OfType<T1>() will return IEnumerable<T1> contains only instances of T1, not T2

+2
source
 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(); } } 
+1
source
 new List<Customer>(myCustomerEnumerator); 
+1
source

The recommended base class for creating your own collection implementations is System.Collections.ObjectModel.Collection<T>

(from MSDN)
This base class is provided to make it easier for developers to create a custom collection. Developers are encouraged to extend this base class, rather than creating their own.

 public class CustomerCollection : Collection<Customer> { } 
+1
source

All Articles