IGrouping的实现类是什么?

时间:2021-12-31 14:31: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.

我正在尝试创建一个WCF数据服务ServiceOperation,它在服务器端进行分组,然后将数据发送到客户端。

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.

我使用的唯一界面是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?

但它的组装是什么?

4 个解决方案

#1


24  

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

BCL中的几种类型实现了IGrouping,但它们都是内部的,除了IGrouping接口之外无法访问。

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:

但是,IGrouping仅仅是具有关联键的IEnumerable 。您可以轻松实现由List 支持的IGrouping,并且不应该难以跨调用边界序列化:

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:

应用GroupBy运算符后,您可以创建分组实例列表:

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

#2


11  

Here is probably the most basic and generic implementation of IGrouping. Its constructor takes a key and a set of values.

这可能是IGrouping最基本和最通用的实现。它的构造函数接受一个键和一组值。

public class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    private readonly TKey key;
    private readonly IEnumerable<TElement> values;

    public Grouping(TKey key, IEnumerable<TElement> values)
    {
        if (values == null)
            throw new ArgumentNullException("values");
        this.key = key;
        this.values = values;
    }

    public TKey Key
    {
        get { return key; }
    }

    public IEnumerator<TElement> GetEnumerator()
    {
        return values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

#3


4  

I checked the type with a sample app and it is this:System.Linq.Lookup<TKey,TElement>.Grouping. But what assembly is it in?

我使用示例应用程序检查了类型,它是:System.Linq.Lookup .Grouping。但它的组装是什么? ,telement>

It's a type nested in System.Linq.Lookup<TKey,TElement>; internal to the System.Core assembly.

它是嵌套在System.Linq.Lookup 中的类型; System.Core程序集内部。 ,telement>

 var groupingType = "1".GroupBy(x => x).Single().GetType().GetGenericTypeDefinition();
 Console.WriteLine("Type: " + groupingType);
 Console.WriteLine("Public: " + groupingType.IsPublic);
 Console.WriteLine("Assembly: " + groupingType.Assembly);

Output:

输出:

Type: System.Linq.Lookup`2+Grouping[TKey,TElement]
Public: False
Assembly: System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

As of .NET 4.0, there is no public type in the core .NET framework that implements System.Linq.IGrouping<TKey,TElement>. If you need such a type (say that's serializable), you'll probably have to roll one yourself, unfortunately.

从.NET 4.0开始,核心.NET框架中没有实现System.Linq.IGrouping 的公共类型。如果你需要这样的类型(比如说可序列化的),你可能不得不自己滚动一个类型。 ,telement>

#4


-4  

System.Core (in System.Core.dll) http://msdn.microsoft.com/en-us/library/bb344977.aspx

System.Core(在System.Core.dll中)http://msdn.microsoft.com/en-us/library/bb344977.aspx

#1


24  

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

BCL中的几种类型实现了IGrouping,但它们都是内部的,除了IGrouping接口之外无法访问。

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:

但是,IGrouping仅仅是具有关联键的IEnumerable 。您可以轻松实现由List 支持的IGrouping,并且不应该难以跨调用边界序列化:

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:

应用GroupBy运算符后,您可以创建分组实例列表:

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

#2


11  

Here is probably the most basic and generic implementation of IGrouping. Its constructor takes a key and a set of values.

这可能是IGrouping最基本和最通用的实现。它的构造函数接受一个键和一组值。

public class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    private readonly TKey key;
    private readonly IEnumerable<TElement> values;

    public Grouping(TKey key, IEnumerable<TElement> values)
    {
        if (values == null)
            throw new ArgumentNullException("values");
        this.key = key;
        this.values = values;
    }

    public TKey Key
    {
        get { return key; }
    }

    public IEnumerator<TElement> GetEnumerator()
    {
        return values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

#3


4  

I checked the type with a sample app and it is this:System.Linq.Lookup<TKey,TElement>.Grouping. But what assembly is it in?

我使用示例应用程序检查了类型,它是:System.Linq.Lookup .Grouping。但它的组装是什么? ,telement>

It's a type nested in System.Linq.Lookup<TKey,TElement>; internal to the System.Core assembly.

它是嵌套在System.Linq.Lookup 中的类型; System.Core程序集内部。 ,telement>

 var groupingType = "1".GroupBy(x => x).Single().GetType().GetGenericTypeDefinition();
 Console.WriteLine("Type: " + groupingType);
 Console.WriteLine("Public: " + groupingType.IsPublic);
 Console.WriteLine("Assembly: " + groupingType.Assembly);

Output:

输出:

Type: System.Linq.Lookup`2+Grouping[TKey,TElement]
Public: False
Assembly: System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

As of .NET 4.0, there is no public type in the core .NET framework that implements System.Linq.IGrouping<TKey,TElement>. If you need such a type (say that's serializable), you'll probably have to roll one yourself, unfortunately.

从.NET 4.0开始,核心.NET框架中没有实现System.Linq.IGrouping 的公共类型。如果你需要这样的类型(比如说可序列化的),你可能不得不自己滚动一个类型。 ,telement>

#4


-4  

System.Core (in System.Core.dll) http://msdn.microsoft.com/en-us/library/bb344977.aspx

System.Core(在System.Core.dll中)http://msdn.microsoft.com/en-us/library/bb344977.aspx