What is the implementing class for IGrouping?

Vaccano picture Vaccano · Dec 14, 2011 · Viewed 16.1k times · Source

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 even connect to the service) I get an error. It says that it can't construct an interface.

The only interface I am using is IGrouping.

What is a/the actual class for this interface?


Update:

I checked the type while debugging a sample app and it told me it was:

System.Linq.Lookup<TKey,TElement>.Grouping

But what assembly is it in?

Answer

Martin Liversage picture Martin Liversage · Dec 14, 2011

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<TElement> with an associated key. You can easily implement an IGrouping that is backed by a List<TElement> and that should not be hard to serialize across a call boundary:

public class Grouping<TKey, TElement> : IGrouping<TKey, TElement> {

  readonly List<TElement> elements;

  public Grouping(IGrouping<TKey, TElement> grouping) {
    if (grouping == null)
      throw new ArgumentNullException("grouping");
    Key = grouping.Key;
    elements = grouping.ToList();
  }

  public TKey Key { get; private set; }

  public IEnumerator<TElement> 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<TKey, TElement>(g)).ToList();