IGrouping 的实现类是什么?

发布于 2024-12-21 00:11:57 字数 323 浏览 0 评论 0原文

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

当我尝试调用它(甚至连接到该服务)时,我收到错误。它说它无法构造接口。

我使用的唯一界面是 IGrouping。

该接口的实际类是什么?


更新:

我在调试示例应用程序时检查了类型,它告诉我它是:

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

但是它在哪个程序集中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-12-28 00:11:57

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

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(); }

}

应用 GroupBy< /code> 运算符,您可以创建 Grouping 实例列表:

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

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();
一场春暖 2024-12-28 00:11:57

这可能是 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();
    }
}

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

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();
    }
}
花之痕靓丽 2024-12-28 00:11:57

我使用示例应用程序检查了类型,它是:System.Linq.Lookup.Grouping。但它在哪个程序集中呢?

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

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

输出:

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

从 .NET 4.0 开始,核心 .NET 框架中没有实现 System.Linq.IGrouping的公共类型。如果您需要这样一种类型(假设它是可序列化的),那么不幸的是,您可能必须自己滚动一个类型。

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

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

 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文