Introduction
This sample shows different uses of Conversion Operators:
Building the Sample
- Open the Program.cs
- Comment or uncomment the desired samples
- Press Ctrl + F5
Description
ToArray
This sample uses ToArray to immediately evaluate a sequence into an array.
C#
Edit|Remove
csharp
public void Linq54() { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending select d; var doublesArray = sortedDoubles.ToArray(); Console.WriteLine("Every other double from highest to lowest:"); for (int d = 0; d < doublesArray.Length; d += 2) { Console.WriteLine(doublesArray[d]); } }
public void Linq54() { double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending select d; var doublesArray = sortedDoubles.ToArray(); Console.WriteLine("Every other double from highest to lowest:"); for (int d = 0; d < doublesArray.Length; d += 2) { Console.WriteLine(doublesArray[d]); } }
Result
Every other double from highest to lowest:
4.1
2.3
1.7
ToList
This sample uses ToList to immediately evaluate a sequence into a List<T>.
C#
Edit|Remove
csharp
public void Linq55() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w select w; var wordList = sortedWords.ToList(); Console.WriteLine("The sorted word list:"); foreach (var w in wordList) { Console.WriteLine(w); } }
public void Linq55() { string[] words = { "cherry", "apple", "blueberry" }; var sortedWords = from w in words orderby w select w; var wordList = sortedWords.ToList(); Console.WriteLine("The sorted word list:"); foreach (var w in wordList) { Console.WriteLine(w); } }
Result
The sorted word list:
apple
blueberry
cherry
ToDictionary
This sample uses ToDictionary to immediately evaluate a sequence and a related key expression into a dictionary.
C#
Edit|Remove
csharp
public void Linq56() { var scoreRecords = new[] { new {Name = "Alice", Score = 50}, new {Name = "Bob" , Score = 40}, new {Name = "Cathy", Score = 45} }; var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name); Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]); }
public void Linq56() { var scoreRecords = new[] { new {Name = "Alice", Score = 50}, new {Name = "Bob" , Score = 40}, new {Name = "Cathy", Score = 45} }; var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name); Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]); }
Result
Bob's score: { Name = Bob, Score = 40 }
OfType
This sample uses OfType to return only the elements of the array that are of type double.
C#
Edit|Remove
csharp
public void Linq57() { object[] numbers = { null, 1.0, "two", 3, "four", 5, "six", 7.0 }; var doubles = numbers.OfType<double>(); Console.WriteLine("Numbers stored as doubles:"); foreach (var d in doubles) { Console.WriteLine(d); } }
public void Linq57() { object[] numbers = { null, 1.0, "two", 3, "four", 5, "six", 7.0 }; var doubles = numbers.OfType<double>(); Console.WriteLine("Numbers stored as doubles:"); foreach (var d in doubles) { Console.WriteLine(d); } }
Result
Numbers stored as doubles:
1
7
Source Code Files
101 LINQ Samples
- Restriction Operators
- Projection Operators
- Partitioning Operators
- Ordering Operators
- Grouping Operators
- Set Operators
- Conversion Operators
- Element Operators
- Generation Operators
- Quantifiers
- Aggregate Operators
- Miscellaneous Operators
- Custom Sequence Operators
- Query Execution
- Join Operators
More Information
For more information, see:
- Conversion Operators (C# Programming Guide) - http://msdn.microsoft.com/en-us/library/09479473(VS.80).aspx
- Conversion Operators - http://msdn.microsoft.com/en-us/library/ms229033.aspx