What is the implementing class for IGrouping?

后端 未结 4 886
逝去的感伤
逝去的感伤 2020-12-03 10:41

I am trying create a WCF Data Services ServiceOperation that does grouping on the server side and then sends the data down to the client.

When I try to call it (or e

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 11:06

    Several types in the BCL implement IGrouping, however they are all internal and cannot be accessed except by the IGrouping interface.

    But an IGrouping is merely an IEnumerable with an associated key. You can easily implement an IGrouping that is backed by a List and that should not be hard to serialize across a call boundary:

    public class Grouping : IGrouping {
    
      readonly List elements;
    
      public Grouping(IGrouping grouping) {
        if (grouping == null)
          throw new ArgumentNullException("grouping");
        Key = grouping.Key;
        elements = grouping.ToList();
      }
    
      public TKey Key { get; private set; }
    
      public IEnumerator GetEnumerator() {
        return this.elements.GetEnumerator();
      }
    
      IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
    
    }
    

    After applying the GroupBy operator you can create a list of Grouping instances:

    var listOfGroups =
      source.GroupBy(x => ...).Select(g => new Grouping(g)).ToList();
    

提交回复
热议问题