简体   繁体   English

通过3个参数区分

[英]DistinctBy 3 Params

I am trying to use C# MoreLinq to try to figure out duplicates. 我正在尝试使用C#MoreLinq尝试找出重复项。

Here is an example. 这是一个例子。 There is a league, within the league the teams. 有一个联盟,联盟内的球队。 Each player in the team can play any position. 团队中的每个玩家都可以担任任何职位。

public sealed class Triad
{ 
    public int Id1 {get;set;}
    public int Id2 {get;set;}
    public int Id3 {get;set;}
}

With this I want to GroupBy 有了这个我想GroupBy

ArrayOfTraids.GroupBy(new { Id1, Id2, Id3}).Select(t => t);

Example

If Id1 = 1, Id2 = 2 and Id3 = 3, in this example we would get 1 Object. 如果Id1 = 1,Id2 = 2,Id3 = 3,则在此示例中,我们将获得1个对象。

The problem I want to solve is, If Id1 = 2, Id2 = 3, Id3 = 1, I want it ALSO to be apart of the same list, since the int values are still 1,2,3 (regardless of the order, they are still the same values). 我要解决的问题是,如果Id1 = 2,Id2 = 3,Id3 = 1,我还希望它也属于同一列表,因为int值仍为1,2,3(无论顺序如何,它们仍然是相同的值)。

I feel this could be very simple but I am unsure. 我觉得这可能很简单,但我不确定。

SOLVED 解决了

Ended up doing something like this 最终做了这样的事情

ArrayOfTriads.Where(t => t.Id1 < t.Id2 && t.Id2 < t.Id3) 

Which eliminated the repeating. 消除了重复。 Closed. 关闭。

Your answer: 你的答案:

ArrayOfTriads.Where(t => t.Id1 < t.Id2 && t.Id2 < t.Id3)

Does not work with the following example. 不适用于以下示例。 It filters out the first two elements instead of selecting one of them: 它过滤掉前两个元素,而不是选择其中一个:

ArrayOfTraids.Add(new Triad(3, 2, 1));
ArrayOfTraids.Add(new Triad(2, 3, 1));
ArrayOfTraids.Add(new Triad(1, 2, 4));
ArrayOfTraids.Add(new Triad(1, 2, 5));

You get: 你得到:

Id1 Id2 Id3
1   2   4
1   2   5

I think that you may want to group by the ordered Id's. 我认为您可能想按订购ID分组。 Here is a way it could be done: 这是可以完成的方法:

ArrayOfTraids
.GroupBy(m => (string.Join(";", new List<int> { m.Id1, m.Id2, m.Id3 }
.OrderBy(n => n)))).Select(m => m.First());

This produces: 这将产生:

Id1 Id2 Id3
3   2   1
1   2   4
1   2   5

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

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