Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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/0/asp.net-mvc/16.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# 选择dictionary值中的distinct作为对象_C#_Asp.net Mvc_C# 4.0 - Fatal编程技术网

C# 选择dictionary值中的distinct作为对象

C# 选择dictionary值中的distinct作为对象,c#,asp.net-mvc,c#-4.0,C#,Asp.net Mvc,C# 4.0,mainDictionary有所有记录,所以我试图在ColorPriceDictionary上获得不同的记录,但我有重复的记录 objProductFrontModel.ColorPriceDictionary = mainDictionary.Select(m => new { m.Value.ColorId, m.Value.ColorText, m.Value.SizeId }) .Distinc

mainDictionary有所有记录,所以我试图在ColorPriceDictionary上获得不同的记录,但我有重复的记录

objProductFrontModel.ColorPriceDictionary =
                    mainDictionary.Select(m => new { m.Value.ColorId, m.Value.ColorText, m.Value.SizeId })
                        .Distinct()
                        .ToDictionary(m => m.ColorId, m => new ProductDetail { ItemText = m.ColorText, ItemId = m.SizeId });
Distinct在类上使用equals方法(IEquatable或IComparable)来确定区分度

当您说new{m.Value.ColorId、m.Value.ColorText、m.Value.SizeId}时,您正在创建一个包含三个未命名成员变量的匿名未命名类。C编译器为匿名类提供的默认equals实现是比较所有三个成员变量。所以,在使用Distinct之后,每个结果实例都有三个变量的唯一组合,但不一定只有colorId

两种解决方案:

1实现一个自定义的IComparator,它只比较元素或元素的colorId

2在定义的元素类上实现Equals和GetHashCode,然后在Select子句中使用它


字典中的所有键值必须是不同的,但您没有选择不同的键值。您正在选择colorID、ColorText和SizeID的不同元组。在这些不同的元组中,ColorID明显存在一些重复的值,这就是为什么会出现错误


我怀疑如果从选择中删除SizeID,这将成功,因为ColorText显然依赖于SizeID。您的另一个选择是将这些元组选择到一个数据结构中,该结构允许重复的ColorID值,如列表。如果这不起作用,我会说你需要退后一步,想一个不同的方法来解决你试图解决的任何问题。

你能展示一些实际的输出示例吗?@Noctis它给了我一个键重复错误我会假设还有其他键,因为如果颜色id是a,则是唯一的,从…开始,这似乎是多余的?既然这里的键是ColorID,其他的键会影响它的键吗?你有重复记录的依据是什么?Id?那么可能的解决方案是什么?@bryknaval可能的解决方案取决于您希望如何处理至少一个ColorId值具有多个SizeId值这一事实。
class MySelectorElement
{
    string ColorId { get; set; }
    string ColorText { get; set; }
    string SizeId { get; set; }

    override bool Equals(object other)
    {
        if (other is MySelectorElement)
        {
            return object.Equals(this.ColorId, ((MySelectorElement)other).ColorId);
        }
        else return false;
    }

    override int GetHashCode()
    {
        return this.ColorId.getHashCode();
    }
}

//...

... Select(new MySelectorElement { ColorId = m.Value.ColorId, ... })
...