简体   繁体   English

使用Linq .Min()获取最小日期

[英]Using Linq .Min() to get Min Date

i'm trying to get min date from a collection 我正在尝试从集合中获取最小日期

someCollection.Min(d => d.Dates.Select(c => c.ReservationDateStart)).FirstOrDefault();

but it returns exception 但它返回异常

At least one object must implement IComparable 至少一个对象必须实现IComparable

based on this i came up with that Min didn't work but how we get min object from collection based on it's property collection of min? 基于此,我想出了Min不起作用,但是如何基于min的属性集合从集合中获取min对象呢?

my collection is something like this 我的收藏是这样的

public class InfoDatesViewModel
    {
        public DateTime OccureFrom { get; set; }
        public DateTime OccureTo { get; set; }
        public string RecurreFrom { get; set; }
        public string RecurreTo { get; set; }
        public int RecurrentType { get; set; }
        public bool IsFullDay { get; set; }
        public int ReservedQuantity { get; set; }
        public ReservedDateViewModel[] Dates { get; set; }
    }

and ReservedDateViewModel is ReservedDateViewModel

public class ReservedDateViewModel
    {
        public bool IsAvailable { get; set; }
        public DateTime ReservationDate { get; set; }
        public DateTime ReservationDateEnd { get; set; }
        public DateTime ReservationDateStart { get; set; }
    }

The problem is that InfoDatesViewModel.Dates is a collection, so you can't compare it directly. 问题在于InfoDatesViewModel.Dates是一个集合,因此您不能直接比较它。

When having a collection of collections, you can flatten them using the SelectMany operator: 拥有集合的集合时,可以使用SelectMany运算符将它们展平:

someCollection.SelectMany(c => c.Dates).Min(d => d.ReservationDateStart);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM