gpt4 book ai didi

c# - 从 BindingList 中删除重复项

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:45 24 4
gpt4 key购买 nike

BindingList有没有去除重复元素的方案?我试过:

 BindingList<Account> accounts = new BindingList<Account>();

accounts.add(new Account("username", "password"));
accounts.add(new Account("username", "password"));

accounts = accounts.Distinct();

上面的代码不起作用,因为 Distinct 正在返回 System.Collections.Generic.IEnumerable<T>而不是 BindingList<T>

最佳答案

BindingList有一个构造函数,它接受 IList<T>你可以转换一个Enumerable<T> to a List .

BindingList<Account> distinctAccounts = new BindingList<Account>(accounts.Distinct().ToList());

作为King King指出Distinct()使用默认的相等比较器

The default equality comparer, Default, is used to compare values of the types that implement the IEquatable generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.

关于c# - 从 BindingList 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com