Last active
December 18, 2015 11:29
-
-
Save josgraha/5776393 to your computer and use it in GitHub Desktop.
filter object enumeration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ObjectFilter | |
{ | |
interface IObjectTest<T> | |
{ | |
bool test(T o); | |
} | |
class CustomEnumerator<T> | |
{ | |
IObjectTest<T> _PredicateObject; | |
public CustomEnumerator(IObjectTest<T> PredicateObject) | |
{ | |
_PredicateObject = PredicateObject; | |
} | |
public IEnumerator<T> getEnumerator(IEnumerable<T> OriginalEnumerable) | |
{ | |
foreach (T CurrentObject in OriginalEnumerable) | |
{ | |
if (_PredicateObject.test(CurrentObject)) | |
{ | |
yield return CurrentObject; | |
} | |
} | |
} | |
} | |
class Employee | |
{ | |
public Employee(string Name, int Score) | |
{ | |
this.Name = Name; | |
this.Score = Score; | |
} | |
public string Name{ get; set; } | |
public int Score{ get; set; } | |
override public String ToString() | |
{ | |
return this.Name + " " + this.Score; | |
} | |
} | |
class EmployeePredicate : IObjectTest<Employee> | |
{ | |
bool IObjectTest<ObjectFilter.Employee>.test(Employee TestingObject) | |
{ | |
if (TestingObject.Score >= 10) | |
return true; | |
else | |
return false; | |
} | |
} | |
public class EmployeeListIterateTest | |
{ | |
public static void Main() | |
{ | |
List<Employee> EmployeeList = new List<Employee>(10); | |
EmployeeList.Add(new Employee("Emp1", 10)); | |
EmployeeList.Add(new Employee("Emp2", 4)); | |
EmployeeList.Add(new Employee("Emp3", 8)); | |
EmployeeList.Add(new Employee("Emp4", 18)); | |
EmployeeList.Add(new Employee("Emp5", 28)); | |
EmployeeList.Add(new Employee("Emp6", 38)); | |
EmployeeList.Add(new Employee("Emp7", 7)); | |
foreach (Employee EmpObj in EmployeeList) | |
Console.WriteLine(EmpObj.ToString()); | |
Console.WriteLine("Filtered Enumerator..."); | |
CustomEnumerator<Employee> CustomIterator = new CustomEnumerator<Employee>(new EmployeePredicate()); | |
IEnumerator<Employee> CustomEnumerator = CustomIterator.getEnumerator(EmployeeList.AsEnumerable<Employee>()); | |
while(CustomEnumerator.MoveNext()) | |
{ | |
Console.WriteLine(CustomEnumerator.Current); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment