小编Mid*_*eks的帖子

查找过去最近的日期

我有Service课。其中一个字段是ServicePrice对象的集合,其中有一个Priceand\xd0\xa1hangeDate字段。

\n
class Service \n{\n    ObservableCollection<ServicePrice> ServicePrices {get; set;}\n    // other fields\n}\n\nclass ServicePrice \n{\n    int Price {get;set;}\n    DateTime ChangeDate {get;set;}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我需要方法来查找在某个时间点相关的服务的价格。

\n
 public int? GetPriceAtDate(DateTime date)\n{\n    // Do something here\n    int? ActualPrice = 0 \n    return ActualPrice;\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

我正在使用 LINQ MinBy 函数找到解决方案:

\n
 public int? GetPriceAtDate(DateTime date)\n{\n    return ServicePrices.MinBy(sp => (sp.ChangeDate - date).Duration()).Price;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但此函数将返回最近的日期,但不一定是过去的日期。从未来获得产品的价格是很奇怪的。

\n

c# linq datetime

2
推荐指数
1
解决办法
173
查看次数

标签 统计

c# ×1

datetime ×1

linq ×1