简体   繁体   English

两个枚举列表的联合不会产生不同的项目?

[英]Union of two lists of enums do not produce distinct items?

With

        var test = new RarityEnum[] { RarityEnum.Uncommon };
        var test2 = new RarityEnum[] { RarityEnum.Uncommon };
        var test3 = test.Union(test2);

Why is the test3 only containing 1 item and not 2?为什么 test3 只包含 1 项而不包含 2?

How should I change this so it properly produces a new list of enums (even if the same enum value is present multiple times)?我应该如何更改它以便正确生成一个新的枚举列表(即使相同的枚举值多次出现)?

I could cast to int before Union then cast back to enum after...is that the cleanest?我可以在 Union 之前转换为 int ,然后在之后转换回 enum ......那是最干净的吗? I feel casting is not so pretty我觉得选角不太好看

From Microsoft's documentation on Union : "This method excludes duplicates from the return set. This is different behavior to the Concat method, which returns all the elements in the input sequences including duplicates."来自Microsoft 关于 Union 的文档:“此方法从返回集中排除重复项。这与Concat方法不同,后者返回输入序列中的所有元素,包括重复项。”

In this example:在这个例子中:

var test = new string[] { "cat" };
var test2 = new string[] { "cat" };
var test3 = test.Concat(test2);
var test4 = test.Union(test2);

test3 ends up being { "cat", "cat" } test3最终成为{ "cat", "cat" }

test4 is just { "cat" } test4只是{ "cat" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM