Linq OrderBy用于对具有相同集合的对象进行分组

pet*_*loy 7 c# linq

我有一组自己包含一组的对象.

private class Pilot
{
    public string Name;
    public HashSet<string> Skills;
}
Run Code Online (Sandbox Code Playgroud)

这是一些测试数据:

public void TestSetComparison()
{
    var pilots = new[] 
    {
        new Pilot { Name = "Smith", Skills = new HashSet<string>(new[] { "B-52", "F-14" }) },
        new Pilot { Name = "Higgins", Skills = new HashSet<string>(new[] { "Concorde", "F-14" }) },
        new Pilot { Name = "Jones", Skills = new HashSet<string>(new[] { "F-14", "B-52" }) },
        new Pilot { Name = "Wilson", Skills = new HashSet<string>(new[] { "F-14", "Concorde" }) },
        new Pilot { Name = "Celko", Skills = new HashSet<string>(new[] { "Piper Cub" }) },
    };
Run Code Online (Sandbox Code Playgroud)

我想OrderBy在Linq中使用,以便:

  • 史密斯和琼斯一起订购是因为他们乘坐的飞机相同
  • 希金斯和威尔逊是一起订购的,因为他们在同一架飞机上飞行
  • 希金斯+威尔逊是否在史密斯+琼斯之前或之后结束并不重要
  • 最好是史密斯在琼斯之前(稳定排序),但这不是太重要

我想我需要实现一个IComparer<Pilot>传入OrderBy但不知道如何处理"无关紧要"方面(上面)和稳定排序.

更新:

我希望输出是相同五个对象的数组,但顺序不同. Pilot

Dmi*_*nko 7

GroupBy您必须为IEqualityComparer<T>要分组的类型(HashSet<string>在您的情况下)实现时,例如

private sealed class MyComparer : IEqualityComparer<HashSet<string>> {
  public bool Equals(HashSet<string> x, HashSet<string> y) {
    if (object.ReferenceEquals(x, y))
      return true;
    else if (null == x || null == y)
      return false;

    return x.SetEquals(y);
  }

  public int GetHashCode(HashSet<string> obj) {
    return obj == null ? -1 : obj.Count;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后使用它:

 IEnumerable<Pilot> result = pilots
    .GroupBy(pilot => pilot.Skills, new MyComparer())
    .Select(chunk => string.Join(", ", chunk
       .Select(item => item.Name)
       .OrderBy(name => name))); // drop OrderBy if you want stable Smith, Jones

 Console.WriteLine(string.Join(Environment.NewLine, result));
Run Code Online (Sandbox Code Playgroud)

结果:

 Jones, Smith
 Higgins, Wilson
 Celko
Run Code Online (Sandbox Code Playgroud)

编辑:如果你想重新编码一个数组,那么添加SelectMany()以便展平分组和最终ToArray():

 var result = pilots
    .GroupBy(pilot => pilot.Skills, new MyComparer())
    .SelectMany(chunk => chunk)
    .ToArray();

 Console.WriteLine(string.Join(", ", result.Select(p => p.Name)));
Run Code Online (Sandbox Code Playgroud)

结果:

 Jones, Smith,
 Higgins, Wilson,
 Celko
Run Code Online (Sandbox Code Playgroud)

请注意,string.join将每个组的名称组合在一行中,即Jones, Smith两者具有相同的技能组合.

将其作为DotNetFiddle运行