C# 列表中的不同列表

C# Distinct List from a List

我有 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; }
}

我想创建另一个具有 distincton_date,type_code,available 的列表

Distinct() 对所有 returns 重复的列产生不同的结果。

你能帮我一下吗?

提前致谢!

您可以使用 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();