C#列表中的独特列表

时间:2015-07-09 07:03:24

标签: c# list distinct

我有 OccupancyResultList 列表

 List<Occupancy> occupancyResultList = new List<Occupancy>();
占用

public partial class Occupancy
{
    public Nullable<System.DateTime> on_date { get; set; }
    public string type_code { get; set; }
    public Nullable<int> available { get; set; }
    public Nullable<decimal> rate { get; set; }
    public string rate_code { get; set; }
    public string publish_flag { get; set; }
}

我想创建另一个列表 on_date,type_code,

不同

Distinct()会生成所有返回重复列的区别结果。

你能给我一个手吗?

提前Thanx!

1 个答案:

答案 0 :(得分:1)

您可以使用匿名类型的GroupBy作为密钥:

occupancyResultList = occupancyResultList
        .GroupBy(x => new { x.on_date, x.type_code, x.available })
        .Select(g => g.First())
        .ToList();

DistinctBy方法:

occupancyResultList = occupancyResultList
        .DistinctBy(x => new { x.on_date, x.type_code, x.available })
        .ToList();