2016-11-05 58 views
1

的名單我有這個Dictionary謂詞使用查找元組

Public TblDic As New Dictionary(Of String, List(Of Tuple(Of Date, Date, Decimal))) 

我需要檢索元組具有2日項目的最高日第3項(十進制)。

我能夠知道日期檢索第三個項目,但我沒能找回最高日期:

Dim LastDate As Date = Date.ParseExact("31/12/2016", "dd/MM/yyyy", 
             New CultureInfo("it-IT"), DateTimeStyles.None) 
Dim LastRate2 As Decimal = TblDic("Legali").Find(Function(D) D.Item2 = LastDate).Item3 

另一種方法可能是將所有元組到一個DataTable,並使用數據表。選擇檢索小數領域是這樣的:

Dim LastRate As Decimal = DT_Rates.Select("EndDate=MAX(EndDate)")(0).Field(Of Decimal)("Rate") 

所以,我的問題是:
1)是有可能使用List.Find不知道最後日期?
2)List.Find和DataTable.Select之間的最佳做法是什麼?

回答

1

可能使用Find ...也許?但是我認爲沒有它就足夠簡單了。

Dim LastRate As Decimal = TblDic. 
    SelectMany(Function(k) k.Value). 
    OrderByDescending(Function(k) k.Item2). 
    First().Item3 

SelectMany()需要許多序列並將它們平整爲一個序列。在這種情況下的序列是字典的值,List(Of Tuple(Of Date, Date, Decimal))

OrderByDescending()定購由謂詞,它返回Tuple(Of Date, Date, Decimal)Date扁平序列。此訂單所有Tuple(Of Date, Date, Decimal)在所有名單,因爲他們被夷爲平地

First()取第一Tuple(Of Date, Date, Decimal)它是由日降

Item3命令後返回Tuple(Of Date, Date, Decimal)

+0

致謝Decimal你的答案,但我有一些問題給你:你有沒有考慮過,如果我使用'Find',我可以避免填充一個'DataTable'?和:是否可以直接使用'TblDic.OrderByDescending'? (我試過但沒有成功)。 – genespos

+0

我的答案與DataTable無關;它對泛型集合是純粹的LINQ。如果您訂購字典,您仍然只需要一個列表字典。儘管這樣做有迂迴的方法來達到你想要的結果,但我能想到的任何事情都比我的回答更復雜。你有沒有嘗試我的答案,如果是的話,它的工作? – djv

+1

你真厲害!這是我誤解讀你的代碼。再次感謝 – genespos