如何根据OrderBy“property”从Collection返回IOrderedEnumerable

时间:2013-10-24 20:45:28

标签: c# linq lambda sql-order-by iorderedenumerable

class Employee
{
    public string Name {get;set;}
    public int employee_id {get;set}
    public int Age {get;set}
}

Class EmployeeCollection : IEnumerable, IEnumerable<Employee>, IOrderedEnumerable<Employee>
{
    public Expression<Func<Employee, dynamic>> SortOrder {get;set;}
    protected Dictionary<int,Employee> EmployeeById = new Dictionary<int,Employee>();
    public void AddEmployee(Employee ePassed)
    {
        EmployeeById[ePassed.employee_id] = ePassed;
    }
    public IEnumerator<Employee> GetEnumerator()
    {
      foreach (int id in EmployeeByID.Keys)
      {
        yield return EmployeeById[id];
      }
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
      return this.GetEnumerator();
    }
    public IOrderedEnumerable<Employee> CreateOrderedEnumerable<TKey>(Func<Employee, TKey> KeySelector, IComparer<TKey> comparer, bool descending)
    {
      if (descending)
          return this.OrderByDescending(KeySelector, comparer);
      else
          return this.OrderBy(KeySelector, comparer);
    }
    public IEnumerable<Employee> OrderedObjects
    {
        if (this.SortOrder == null)
            return (IEnumerable<Employee>)this;  // No Sort Order applied
        else
        {
          // How do I get the "parameters" from SortOrder to pass into CreateOrderedEnumerable?
          throw new NotImplementedException();
        }
    }
}

我希望能够使用类似于以下语法...

EmployeeCollection.SortOrder = (x => x.Name);
foreach (Employee e in EmployeeCollection.OrderedObjects)
{
  // Do something in the selected order
}

有成千上万的例子说明如何将排序,过滤等结果投入到新的List,Collection,ObservableCollection等中,但如果您的现有集合已经响应事件,则会自动添加新对象以响应通知,用户操作,从服务器等进入的新数据然后所有功能都“丢失”或必须“添加”以使新的List,Collection,ObservableCollection等听取原始集合以便以某种方式保持同步所有各种更新,属性等原始集合ALREADY了解并处理...我希望能够让ORIGINAL“EmployeeCollection”简单地在请求的SortOrder中删除“Employee”对象......

我根据想要使SortOrder属性的语法类似于lambda表达式的orderby部分,并且团队中的其他开发人员习惯使用它来对“SortOrder”属性的语法进行“疯狂猜测”。通过查看System.Linq.Enumerable中的扩展方法,类似于以下内容: public static IOrderedEnumerable<TSource> OrderBy<ToSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);

我是Linq,lambda等的新手,如果我错过了其他人认为显而易见的Linq / Expression Trees,Predicates,匿名代表等的一些关键方面,请提前道歉。

1 个答案:

答案 0 :(得分:1)

当你不使用ThenBy类型的操作时(这是IOrderedEnumerable<T>接口真正添加的),这是有效的。有关支持该功能的解决方案,请参阅C#: How to implement IOrderedEnumerable<T>

public class Employee
{
    public string Name {get;set;}
    public int employee_id {get;set;}
    public int Age {get;set;}
}

public class EmployeeCollection : IEnumerable, IEnumerable<Employee>, IOrderedEnumerable<Employee>
{
    public Func<Employee, object> SortOrder {get;set;}
    public Func<Employee, bool> Filter {get;set;}
    protected Dictionary<int,Employee> EmployeeById = new Dictionary<int,Employee>();
    public void Add(Employee ePassed)
    {
        EmployeeById[ePassed.employee_id] = ePassed;
    }
    public IEnumerator<Employee> GetEnumerator()
    {
        var employees = EmployeeById.Keys.Select(id => this.GetEmployee(id));
        if (Filter != null)
            employees = employees.Where(Filter);
        if (SortOrder != null)
            employees = employees.OrderBy(SortOrder);
        return employees.GetEnumerator();
    }
    public Employee GetEmployee(int id)
    {
        return EmployeeById[id];
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
      return this.GetEnumerator();
    }
    public IOrderedEnumerable<Employee> CreateOrderedEnumerable<TKey>(Func<Employee, TKey> KeySelector, IComparer<TKey> comparer, bool descending)
    {
      throw new NotImplementedException();
    }
}

// this is some code you might use to test this:
var EmployeeCollection = new EmployeeCollection
{
    new Employee { employee_id = 1, Age = 20, Name = "Joe" },
    new Employee { employee_id = 2, Age = 30, Name = "Thomas" },
    new Employee { employee_id = 3, Age = 25, Name = "Robert" },
};
EmployeeCollection.SortOrder = x => x.Age;
EmployeeCollection.Filter = x => x.Name.Length > 4;
foreach (Employee e in EmployeeCollection)
{
    // do something
}