选择字典值中的唯一对象作为对象

Select distinct in dictionary value as object
2021-02-23
  •  译文(汉语)
  •  原文(英语)

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 });
速聊1:
您能否显示一些实际的输出示例?
速聊2:
它给我密钥重复错误
速聊3:
我假设还有其他一些键,因为如果颜色id是唯一的,那么.Distincit从...开始似乎是多余的?
速聊4:
因为这里的键是ColorID,其他因素还会影响它的键吗?
速聊5:
您有基于什么的重复记录?ID?
解决过程1

词典中的所有键值都必须是唯一的,但是您不必选择不同的键值.您正在选择colorID,ColorText和SizeID的不同元组.在这些不同的元组中,明显有ColorID的某些重复值,这就是为什么会出错的原因.

我怀疑如果您从选择中删除SizeID,这将成功,因为ColorText似乎显然取决于SizeID.您的另一选择是将这些元组选择为可容纳ColorID的重复值(如列表)的数据结构.如果那行不通,我想您需要退后一步,想出一种其他方法来解决您要解决的任何问题.

速聊1:
那么可能的解决方案是什么?
速聊2:
可能的解决方案取决于您要如何处理以下事实:至少一个ColorId值具有多个SizeId值.
解决过程2

Distinct使用该equals方法IEquatableIComparable在类上确定不同性.

当您说时new { m.Value.ColorId, m.Value.ColorText, m.Value.SizeId },您正在创建带有三个未命名成员变量的匿名(未命名)类.equalsC#编译器为匿名类提供的默认实现是比较所有三个成员变量.因此,在您之后Distinct,每个结果实例都具有三个变量的唯一组合,但不一定只是colorId.

两种解决方案:

1)执行一个IComparator<T>仅比较colorId元素OR的自定义或

2)在定义的类上实现Equals,然后在子句中使用它.GetHashCodeElementSelect

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, ... })
...

mainDictionary has all records so I am trying to get distinct records here on ColorPriceDictionary but I am having duplicate records

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 });
Talk1:
could you show some actual example of output ?
Talk2:
Its giving me key repetation error
Talk3:
I'll assume there's some other key, since if the color id's a are unique, the .Distincit seems redundant to begin with ... ?
Talk4:
since key here is ColorID does other affect its key??
Talk5:
you are having duplicate records on basis of what? Id?
Solutions1

All key values in a dictionary must be distinct, but you aren't selecting distinct key values. You're selecting distinct tuples of colorID, ColorText, and SizeID. Within these distinct tuples, there are obvously some duplicate values for ColorID, which is why you're getting an error.

I suspect that if you remove SizeID from your selection, this will succeed, since ColorText seems obviously dependent on SizeID. Your other choice is to select these tuples into a data structure that tolerates duplicate values of ColorID, like a list. If that won't work, I'd say you need to take a step back and think about a different approach to whatever problem you're trying to solve.

Talk1:
so what may be the possible solution?
Talk2:
the possible solution depends on how you want to handle the fact that you have more than one SizeId value for at least one ColorId value.
Solutions2

Distinct uses the equals method, IEquatable or IComparable on the class to determine distinctness.

When you say new { m.Value.ColorId, m.Value.ColorText, m.Value.SizeId }, you are creating an anonymous (unnamed) class with three unnamed member variables. The default equals implementation provided by the C# compiler for anonymous classes is to compare all three member variables. So after your Distinct, every result instance has a unique combination of the three variables, but not necessarily just colorId.

Two solutions:

1) mplement a custom IComparator<T> which compares just the colorId of the element OR

2) Implement Equals and GetHashCode on a defined Element class and then use that in the Select clause.

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, ... })
...
转载于:https://stackoverflow.com/questions/23692668/select-distinct-in-dictionary-value-as-object

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入