Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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# 将自定义选择器委托传递给LINQ的GroupBy?_C#_Linq_Group By - Fatal编程技术网

C# 将自定义选择器委托传递给LINQ的GroupBy?

C# 将自定义选择器委托传递给LINQ的GroupBy?,c#,linq,group-by,C#,Linq,Group By,所以我有一个扩展,它返回一个重复项的KeyValuePair键和IEnumerable中相应的索引值 但是我在更新KeyValuePair时遇到了无效参数错误 似乎我对所有应该传递的通用参数都感到困惑——我不知道如何连接它们:s 我认为这里的问题是选择器应该返回第一个Select投影的匿名类型,而不是TKey,但是我如何才能注入匿名类型泛型arg呢 有什么想法吗 谢谢 编辑: 找到了一个解决方法-不是很好,但它很有效,并且显示了我想要的东西 private static List<

所以我有一个扩展,它返回一个重复项的KeyValuePair键和IEnumerable中相应的索引值

但是我在更新KeyValuePair时遇到了无效参数错误

似乎我对所有应该传递的通用参数都感到困惑——我不知道如何连接它们:s

我认为这里的问题是选择器应该返回第一个Select投影的匿名类型,而不是TKey,但是我如何才能注入匿名类型泛型arg呢

有什么想法吗

谢谢

编辑:

找到了一个解决方法-不是很好,但它很有效,并且显示了我想要的东西

    private static List<TSource> GetDups<TSource, TKey>(List<TSource> source, Func<TSource, TKey> selector)
    {
        var dupGosIndices = source
            .Select(selector)
            .GetDuplicates().Select(x => x.Value);

        var dupGos = new List<TSource>();
        foreach (IEnumerable<int> indicesList in dupGosIndices) {
            dupGos.Add(source[indicesList.ElementAt(0)]);
        }
        return dupGos;
    }
试试这个?IGrouping上的分组都将具有相同的TSource值,因此继续并获取第一个值

            return e.Select((value, index) => new { Index = index, Value = value })
                .GroupBy(x => selector(x.Value))
                .Select(xg => new KeyValuePair<TSource, int[]>(xg.Select(x => x.Value).First(), xg.Select(x => x.Index).ToArray()))
                .Where(kv => kv.Value.Length > 1);

您是如何定义选择器的?什么是退货类型?嗯代码Func selector返回TKey-我从GroupBy的选择器中获得了值你能显示选择器代码吗?如果我理解正确,选择器的意思是,请将在该属性上复制的副本还给我?对的
    private static List<TSource> GetDups<TSource, TKey>(List<TSource> source, Func<TSource, TKey> selector)
    {
        var dupGosIndices = source
            .Select(selector)
            .GetDuplicates().Select(x => x.Value);

        var dupGos = new List<TSource>();
        foreach (IEnumerable<int> indicesList in dupGosIndices) {
            dupGos.Add(source[indicesList.ElementAt(0)]);
        }
        return dupGos;
    }
            return e.Select((value, index) => new { Index = index, Value = value })
                .GroupBy(x => selector(x.Value))
                .Select(xg => new KeyValuePair<TSource, int[]>(xg.Select(x => x.Value).First(), xg.Select(x => x.Index).ToArray()))
                .Where(kv => kv.Value.Length > 1);