在C#中创建一个独特的自定义类型列表

我收到一个entity framework类型列表,并希望只返回List中的不同值。 我正在使用以下方法,但它并没有统一列表。 有什么建议?

Param: List flags

 List distinctFlags = flags.Distinct().ToList(); 

Flag的值如下:ID,Flag,FlagValue。 我可以在这个例子中使用linq吗?

谢谢。

假设Flag是您的实体模型之一,您可以使用partial class并覆盖EqualsGetHashCode 。 这也假设您在Flag class上有一个Id属性,它唯一地标识它。

 //this namespace MUST match the namespace of your entity model. namespace Your.Entity.Model.Namespace { public partial class Flag { public override bool Equals(object obj) { var item = obj as Flag; if (item == null) { return false; } return this.Id.Equals(item.Id); } public override int GetHashCode() { return this.Id.GetHashCode(); } } } 

用法看起来像这样

 List distinctFlags = allFlags.Distinct().ToList(); 

可能flags是一个引用类型的列表,而distinct不会像你期望的那样工作! 这是因为Distinct()不依赖于列表中的标志值,而是处理其内存引用(即所有不同的值)。

你必须编写一个比较器类来教授如何比较等标志。 假设你有这个标志类:

 public class flag { public string Name { get; set; } public string Code { get; set; } } 

你应该创建一个这样的比较器类:

 class FlagComparer : IEqualityComparer { // Products are equal if their names and product numbers are equal. public bool Equals(flag x, flag y) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; //Check whether the products' properties are equal. return x.Code == y.Code && x.Name == y.Name; } } 

并致电你的声明:

 List distinctFlags = flags.Distinct(new FlagComparer ()).ToList(); 

通过这种方式,Distinct方法确切地知道如何比较equals flag istance。

UPDATE

根据您的评论,如果您想按照我的建议,您应该编写比较基数如下:

 class FlagComparer : IEqualityComparer { // Products are equal if their names and product numbers are equal. public bool Equals(flag x, flag y) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; //Check whether the products' properties are equal. return x.HostID == y.HostID && x.RuleID == y.RuleID && x.Flag == y.Flag && x.FlagValue == y.FlagValue; } } 

当然,每个属性都必须是值类型。

看看这里澄清自己:

  • 值类型
  • 参考类型