分组列表<字符串>c# 中的类似类型

发布于 2024-11-17 12:08:23 字数 131 浏览 2 评论 0原文

.GroupBy(x => x) 

.GroupBy 用于对相似类型的 string、int 等进行分组。 那么我可以使用什么函数来对 List< 进行分组?字符串>类似类型的。

.GroupBy(x => x) 

.GroupBy is used to group string,int,etc of similar types.
Then what function can i use to group List< string> of similar type.

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

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

发布评论

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

评论(2

温折酒 2024-11-24 12:08:23

GroupBy 依赖于以适当的方式实现 Equals/GetHashCode 的元素类型来实现您的目标。

您的问题不清楚,但我的猜测是您希望两个具有相同元素的列表被视为相等。我怀疑您需要编写自己的 IEqualityComparer 实现,并将其传递到 GroupBy 中。例如(未经测试):

public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
    private static readonly EqualityComparer<T> ElementComparer =
        EqualityComparer<T>.Default;

    public int GetHashCode(List<T> input)
    {
        if (input == null)
        {
            return 0;
        }
        // Could use Aggregate for this...
        int hash = 17;
        foreach (T item in input)
        {
            hash = hash *31 + ElementComparer.GetHashCode(item);
        }
        return hash;
    }

    public bool Equals(List<T> first, List<T> second)
    {
        if (first == second)
        {
            return true;
        }
        if (first == null || second == null)
        {
            return false;
        }
        return first.SequenceEqual(second, ElementComparer);
    }
}

您还可以允许每个 ListEqualityComparer 实例具有单独的每个元素比较器,这将允许您以不区分大小写的方式比较(例如)字符串列表。

GroupBy relies on the element type implementing Equals/GetHashCode in an appropriate way for your aim.

Your question isn't clear, but my guess is that you want two lists with the same elements to be considered equal. I suspect you'll need to write your own IEqualityComparer implementation, and pass that into GroupBy. For example (untested):

public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
    private static readonly EqualityComparer<T> ElementComparer =
        EqualityComparer<T>.Default;

    public int GetHashCode(List<T> input)
    {
        if (input == null)
        {
            return 0;
        }
        // Could use Aggregate for this...
        int hash = 17;
        foreach (T item in input)
        {
            hash = hash *31 + ElementComparer.GetHashCode(item);
        }
        return hash;
    }

    public bool Equals(List<T> first, List<T> second)
    {
        if (first == second)
        {
            return true;
        }
        if (first == null || second == null)
        {
            return false;
        }
        return first.SequenceEqual(second, ElementComparer);
    }
}

You could also allow each ListEqualityComparer instance to have a separate per-element comparer, which would allow you to compare (say) lists of strings in a case-insensitive fashion.

圈圈圆圆圈圈 2024-11-24 12:08:23

我假设您问如何按自定义类型的对象进行分组。

您需要定义如何将对象相互比较。
您可以通过在 GroupBy 调用中指定 IEqualityComparer 来完成此操作。

如果未指定 IEqualityComparer,则使用 IEqualityComparer.Default,检查类型 T 是否实现 System.IEquatableT> 接口,如果是,则返回使用该实现的 EqualityComparer。否则,它返回一个 EqualityComparer,它使用 T 提供的 Object.Equals 和 Object.GetHashCode 重写。

I assume you are asking how to group by objects of custom type.

You need to define how your objects should be compared to each other.
You can do this by specifying IEqualityComparer in the GroupBy call.

If you don't specify IEqualityComparer, then IEqualityComparer.Default is used, which checks whether type T implements the System.IEquatable<T> interface and, if so, returns an EqualityComparer<T> that uses that implementation. Otherwise, it returns an EqualityComparer<T> that uses the overrides of Object.Equals and Object.GetHashCode provided by T.

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