Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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/2/.net/20.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# 如何按列表分组<;int>;在字典中<;字符串,列表<;int>&燃气轮机;这些清单在哪里是相同的?_C#_.net_Linq - Fatal编程技术网

C# 如何按列表分组<;int>;在字典中<;字符串,列表<;int>&燃气轮机;这些清单在哪里是相同的?

C# 如何按列表分组<;int>;在字典中<;字符串,列表<;int>&燃气轮机;这些清单在哪里是相同的?,c#,.net,linq,C#,.net,Linq,我想使用字典,然后为字典中的所有重复列表创建组 Dictionary<string, List<int>> AllLists = new Dictionary<string, List<int>>() { {"one", new List<int>() {1, 2, 3, 4, 5}}, {"two", new List<int>() {1, 2, 3, 4, 5}}, {"three", new L

我想使用
字典
,然后为字典中的所有重复列表创建组

Dictionary<string, List<int>> AllLists = new Dictionary<string, List<int>>()
{
    {"one", new List<int>() {1, 2, 3, 4, 5}},
    {"two", new List<int>() {1, 2, 3, 4, 5}},
    {"three", new List<int>() {1, 1, 1, 1, 1}}
};

var ListGroups = AllLists.GroupBy(p => p.Value);
Dictionary alllist=new Dictionary()
{
{“一”,新列表(){1,2,3,4,5},
{“两个”,新列表(){1,2,3,4,5},
{“三”,新列表(){1,1,1,1}
};
var ListGroups=AllLists.GroupBy(p=>p.Value);

这应该将具有匹配列表的字典索引分组到它们自己的组中,但它只是为字典中的每个索引创建一个组。我做错了什么?

这将在您的
列表
对象上使用引用比较。由于包含
[1,2,3,4,5]
列表和
都是单独实例化的,因此它们将具有不同的引用

例如,请尝试以下操作:

var ListGroups = AllLists.GroupBy(p => string.Join(",", p.Value));
这将根据列表的
string
表示进行分组。请注意,这可能不是您想要做的,并且纯粹是演示性的

您可以使用
GroupBy
方法传入一个自定义的
IEqualityComparer
,它实际使用
Enumerable.SequenceEqual
查看列表的内容

以下是
IEqualityComparer

类IEnumerableQualityComparer:IEqualityComparer
{
公共布尔等于(IEnumerable a,IEnumerable b)
{
返回可枚举的SequenceEqual(a,b);
}
public int GetHashCode(IEnumerable源代码)
{
if(source==null)
{
返回0;
}
int-shift=0;
int结果=1;
foreach(源中的var项)
{
int hash=item!=null?item.GetHashCode():17;
结果^=(哈希>(32-shift))

& (1)因为编译器不知道如何比较您的列表。那么,我可以做些什么来修复它@Selman22@DavidMyers,那么您想删除重复的条目吗?实际上,我只是想将其分组。我稍后会合并所有重复的列表,但到目前为止,该部分工作正常。@AmitJokiI担心它正在比较引用的es…我试过这个,我也试过p.Value.ToString(),但没有用。只要我能将它分成不同的组,它肯定会有用。@DavidMyers“我试过这个。”你确定吗?使用
string.Join(“,”,p.Value)
您的密钥将起作用。这个答案省去了最困难的部分,即如何正确地对项目序列进行散列。您的散列函数会在序列中抛出空项目,除此之外,这很好。这非常有效!非常感谢!@TimothyShields
class IEnumerableEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
    public bool Equals(IEnumerable<T> a, IEnumerable<T> b)
    {
        return Enumerable.SequenceEqual(a, b);
    }

    public int GetHashCode(IEnumerable<T> source)
    {
        if (source == null)
        {
            return 0;
        }
        int shift = 0;
        int result = 1;
        foreach (var item in source)
        {
            int hash = item != null ? item.GetHashCode() : 17;
            result ^= (hash << shift)
                    | (hash >> (32 - shift))
                    & (1 << shift - 1);
            shift = (shift + 1) % 32;
        }
        return result;
    }
}
var ListGroups = AllLists.GroupBy(p => p.Value,
    new IEnumerableEqualityComparer<int>());