我对LINQ比较陌生,可以使用一些帮助。给出以下结构:
Public Class BaseRate
Public Property EffectiveDate As DateTime
Public Property PayRate As Decimal
End Class
我有一个IEnumerable(Of BaseRate)
可以容纳几个物体的物体。集合中可能有重复日期的数据,有些可能没有。我的业务要求如下:
BaseRate
带有EffectiveDate.Date > objDate.Date
(objDate
在函数中提供)的对象EffectiveDate.Date
从集合中获取“最高”对象。BaseRate
与“最高”匹配的对象EffectiveDate.Date
我以为我正在接近以下几点:
Dim distinct_dates As IEnumerable(Of IGrouping(Of DateTime, BaseRate)) _
= Me.GroupBy(Function(o) o.EffectiveDate.Date)
Dim active_at_date As IEnumerable(Of BaseRate) = distinct_dates.Select( _
Function(grp) grp.OrderByDescending( _
Function(o) o.EffectiveDate.Date).Where( _
Function(o) o.EffectiveDate.Date <= objDate.Date))
但是,我收到此错误:
无法转换类型为“ WhereSelectEnumerableIterator`2 [System.Linq.IGrouping`2 [System.DateTime,BaseRate],System.Collections.Generic.IEnumerable`1 [BaseRate]]”的类型为“ System.Collections.Generic.IEnumerable”的对象'1 [BaseRate]'。
我确定我有错误的顺序,但不确定是什么。
我在VB.NET和方法语法中做到了这一点,但可以在其他任何一个中随意回答。我可以根据需要翻译。
我认为您只是错过了FirstOrDefault
电话。
LINQ在C#中看起来更加清晰,所以我的答案将在C#中,正如您所说的那样,您可以将其转换为VB。
var results = source.Where(x => x.EffectiveDate.Date > objDate.Date)
.GroupBy(x => x.EffectiveDate)
.OrderByDescending(g => g.Key)
.FirstOrDefault()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句