按字段删除结构列表

发布于 2024-12-03 01:28:11 字数 62 浏览 0 评论 0原文

我在 C# 中有一个列表,该列表包含结构,我想删除重复的结构,但只删除某些字段相等的结构。 我该怎么办? 谢谢

I have a list in c#, that list contains structures, I would like to delete repeated structures, but just the structures which have some fields equal.
How Can I do?
thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2024-12-10 01:28:11
List<Sample> samples = new List<Sample>(new[]
{
    new Sample {Id = 1},
    new Sample {Id = 1},
    new Sample {Id = 2},
    new Sample {Id = 3},
    new Sample {Id = 1}
});

var duplicates = samples
    .Select    ((s, i) => new { s.Id, Index = i })  // Get item key and index
    .GroupBy   (s => s.Id)                          // Group by Key
    .Where     (g => g.Count() > 1)                 // Get duplicated ones
    .SelectMany(g => g.Skip (1)                     // We'll keep first one
                      .Select(i => i.Index))        // Get other items ids
    .Reverse();
foreach (var index in duplicates)
{
    samples.RemoveAt(index);
}
List<Sample> samples = new List<Sample>(new[]
{
    new Sample {Id = 1},
    new Sample {Id = 1},
    new Sample {Id = 2},
    new Sample {Id = 3},
    new Sample {Id = 1}
});

var duplicates = samples
    .Select    ((s, i) => new { s.Id, Index = i })  // Get item key and index
    .GroupBy   (s => s.Id)                          // Group by Key
    .Where     (g => g.Count() > 1)                 // Get duplicated ones
    .SelectMany(g => g.Skip (1)                     // We'll keep first one
                      .Select(i => i.Index))        // Get other items ids
    .Reverse();
foreach (var index in duplicates)
{
    samples.RemoveAt(index);
}
不可一世的女人 2024-12-10 01:28:11

有两种可能的解决方案:

  1. 手动删除重复项:这意味着使用嵌套循环迭代列表。
  2. 为结构分配哈希码和相等性检查,并使用 Hashset 删除重复项。这可以通过自定义 IEqualityComparer 来完成(链接) 实现,或者如果您通过使用适当的 GetHashCodeEquals 方法重写实现 IEquatable 接口来“拥有”该结构。

如果你的集合很小并且这个操作必须在你的代码中完成一次,我会选择解决方案一。但如果一遍又一遍地使用这种比较逻辑,我会选择解决方案二。

解决方案二的实现:

    struct YourStruct
    {
       public int Id; 
    }

    class Comparer : IEqualityComparer<YourStruct>
    {

      public bool Equals(YourStruct a, YourStruct b)
      {
        return a.Id == b.Id;
      }


      public int GetHashCode(YourStruct s)
      {
        return s.Id;
      }

    }

    List<YourStruct> list = new List<YourStruct>();

    HashSet<YourStruct> hs = new HashSet<YourStruct>(list, new Comparer());

There are two possible solution:

  1. Remove the duplicates by hand: meaning iterate through the list with a nested loop.
  2. Assign the struct a hash code and equality check and use a Hashset<YourStruct> to remove the duplicates. This can be done by a custom IEqualityComparer (link) implementation or if you "own" the struct by implementing the IEquatable interface with appropriate GetHashCode and Equals method overriding.

If your set is small and this operation has to be done once in your code, I would go for solution one. But if this comparison logic is used over and over again I would go for solution two.

Implementation for solution two:

    struct YourStruct
    {
       public int Id; 
    }

    class Comparer : IEqualityComparer<YourStruct>
    {

      public bool Equals(YourStruct a, YourStruct b)
      {
        return a.Id == b.Id;
      }


      public int GetHashCode(YourStruct s)
      {
        return s.Id;
      }

    }

    List<YourStruct> list = new List<YourStruct>();

    HashSet<YourStruct> hs = new HashSet<YourStruct>(list, new Comparer());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文