使用 LINQ 查找多个属性中的重复项

发布于 2024-10-31 14:37:57 字数 507 浏览 1 评论 0原文

给定一个具有以下定义的类:

public class MyTestClass
{
    public int ValueA { get; set; }
    public int ValueB { get; set; }
}

如何在 MyTestClass[] 数组中找到重复值?

例如,

MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };

包含重复项,因为有两个 MyTestClass 对象,其中 ValueA ValueB 均 = 1

Given a class with the following definition:

public class MyTestClass
{
    public int ValueA { get; set; }
    public int ValueB { get; set; }
}

How can duplicate values be found in a MyTestClass[] array?

For example,

MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };

Contains a duplicate as there are two MyTestClass objects where ValueA and ValueB both = 1

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

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

发布评论

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

评论(3

寒尘 2024-11-07 14:37:57

您可以通过按 ValueA 和 ValueB 对元素进行分组来查找重复项。
之后对它们进行计数,您会发现哪些是重复的。

这是隔离受骗者的方法:

var duplicates = items.GroupBy(i => new {i.ValueA, i.ValueB})
  .Where(g => g.Count() > 1)
  .Select(g => g.Key);

You can find your duplicates by grouping your elements by ValueA and ValueB.
Do a count on them afterwards and you will find which ones are duplicates.

This is how you would isolate the dupes :

var duplicates = items.GroupBy(i => new {i.ValueA, i.ValueB})
  .Where(g => g.Count() > 1)
  .Select(g => g.Key);
失退 2024-11-07 14:37:57

您可以同时使用 Jon Skeet 的 DistinctByExcept 来查找重复项。请参阅此回复了解他对 DistinctBy< 的解释/代码>。

MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };

MyTestClass [] distinctItems = items.DistinctBy(p => new {p.ValueA, p.ValueB}).ToArray();
MyTestClass [] duplicates = items.Except(distinctItems).ToArray();

但它只会返回一项,而不返回两项重复项。

You could just use Jon Skeet's DistinctBy and Except together to find duplicates. See this Response for his explanation of DistinctBy.

MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };

MyTestClass [] distinctItems = items.DistinctBy(p => new {p.ValueA, p.ValueB}).ToArray();
MyTestClass [] duplicates = items.Except(distinctItems).ToArray();

It will only return one item and not both duplicates however.

别靠近我心 2024-11-07 14:37:57

MyTestClass 应该实现 Equals 方法。

public bool Equals(MyTestClass x, MyTestClass y)
{
    if (Object.ReferenceEquals(x, y)) return true;

    if (Object.ReferenceEquals(x, null) ||
        Object.ReferenceEquals(y, null))
            return false;

        return x.ValueA == y.ValueA && y.ValueB == y.ValueB;
}

这里有一个 关于它的好文章

之后,您可以使用“Distinct”方法获得 MyTestClass 的“干净”列表。

MyTestClass should implement the Equals method.

public bool Equals(MyTestClass x, MyTestClass y)
{
    if (Object.ReferenceEquals(x, y)) return true;

    if (Object.ReferenceEquals(x, null) ||
        Object.ReferenceEquals(y, null))
            return false;

        return x.ValueA == y.ValueA && y.ValueB == y.ValueB;
}

Here you have a good article about it.

After that you can get a "clean" list of MyTestClass with "Distinct" method.

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