LINQ To DataSet Samples - Conversion OperatorsOn This Page |
This sample uses ToArray to immediately evaluate a sequence into an array.
Public Sub DataSetLinq54()
Dim doublesDataTable = TestDS.Tables("Doubles").AsEnumerable()
Dim doubles = From d In doublesDataTable _
Select d!double
Dim doublesArray = doubles.ToArray()
Console.WriteLine("Every other double from highest to lowest:")
For d = 0 To doublesArray.Length
Console.WriteLine(doublesArray(d))
d += 1
Next
End Sub
Result:
Every other double from highest to lowest:
2
2
3
This sample uses ToList to immediately evaluate a sequence into a List(Of T).
Public Sub DataSetLinq55()
Dim wordsTable = TestDS.Tables("Words").AsEnumerable()
Dim words = From w In wordsTable _
Select w!word
Dim wordList = words.ToList()
Console.WriteLine("The sorted word list:")
For Each w In wordList
Console.WriteLine(w)
Next
End Sub
Result:
The sorted word list:
aPPLE
BlUeBeRrY
cHeRry
This sample uses ToDictionary to immediately evaluate a sequence and a related key expression into a dictionary.
Public Sub DataSetLinq56()
Dim scoreRecords = TestDS.Tables("ScoreRecords").AsEnumerable()
Dim scoreRecordsDict = scoreRecords.ToDictionary(Function(sr) sr("Name"))
Console.WriteLine("Bob's score: {0}", scoreRecordsDict("Bob")("Score"))
End Sub
Result:
Bob's score: 40
This sample uses OfType to return only the rows of the DataTable containing a value of type double.
Public Sub DataSetLinq57()
Dim numbers = TestDS.Tables("MixedNumbers").AsEnumerable()
Dim doubles = From n In numbers _
Where TypeOf n!number Is Double _
Select n
Console.WriteLine("Numbers stored as doubles:")
For Each d In doubles
Console.WriteLine(d!number)
Next
End Sub
Result:Numbers stored as doubles: 1 7