Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iGroup的实现类是什么?_C#_.net_Wcf Data Services - Fatal编程技术网

C# iGroup的实现类是什么?

C# iGroup的实现类是什么?,c#,.net,wcf-data-services,C#,.net,Wcf Data Services,我正在尝试创建一个WCF数据服务服务操作,该操作在服务器端进行分组,然后将数据发送到客户端 当我尝试调用它(甚至连接到服务)时,我会得到一个错误。它说它不能构造接口 我使用的唯一接口是iGroup 这个接口的实际类是什么 更新: 我在调试示例应用程序时检查了类型,它告诉我它是: System.Linq.Lookup<TKey,TElement>.Grouping System.Linq.Lookup.Grouping 但它是在哪个组件中 我用一个示例应用程序检查了类型,结果是:

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

当我尝试调用它(甚至连接到服务)时,我会得到一个错误。它说它不能构造接口

我使用的唯一接口是iGroup

这个接口的实际类是什么


更新:

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

System.Linq.Lookup<TKey,TElement>.Grouping
System.Linq.Lookup.Grouping
但它是在哪个组件中

我用一个示例应用程序检查了类型,结果是:
System.Linq.Lookup.Grouping
。但它是在哪个组件中

它是嵌套在
System.Linq.Lookup
中的类型;
系统核心的内部组件

 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


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

BCL中的几个类型实现了
i分组
,但是它们都是内部的,只能通过
i分组
接口访问

但是
i分组
仅仅是一个带有相关键的
i可数
。您可以轻松实现由
列表支持的
i分组
,并且跨调用边界进行序列化并不困难:

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

}

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

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();
    }
}
公共类分组:i分组
{
专用只读TKey密钥;
私有只读IEnumerable值;
公共分组(TKey、IEnumerable值)
{
如果(值==null)
抛出新的ArgumentNullException(“值”);
this.key=key;
这个值=值;
}
公钥
{
获取{return key;}
}
公共IEnumerator GetEnumerator()
{
返回值。GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}

请参阅WCF数据服务服务操作?或者WCF操作?System.Core拥有所有LINQ实现。@JeffN825-我忘了澄清这一点。这是一个WCF数据服务服务操作。我更新了我的question.System.Core(在System.Core.dll中),最简单的方法可能是:var newGrouping=newlist().GroupBy(t=>t.MyKey)@aBetterGamer:我认为问题不在于如何创建一个
iGroup
,正如您所指出的,这很容易,而在于如何在序列化边界的另一端重新创建实现
iGroup
的“东西”。答案帮助我理解
iGroup
如何在内部产生多个值+1。
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();
    }
}