Conversion operations change the type of input objects.
AsEnumerable
Returns the input typed as IEnumerable.
Method Syntax
// AsEnumerable int[] numbers = { 1, 2, 3, 4, 5 }; var nums = numbers.AsEnumerable(); // nums: static type is IEnumerable<int>
Query Syntax
// Not applicable.
AsQueryable
Converts a IEnumerable to a IQueryable.
Method Syntax
// AsQueryable int[] numbers = { 1, 2, 3, 4, 5 }; var nums = numbers.AsQueryable(); // nums: static type is IQueryable<int>
Query Syntax
// Not applicable.
Cast
Casts the elements of a collection to a specified type.
Method Syntax
// Cast var numbers = new object[] { 1, 2, 3, 4, 5 }; var nums = numbers.Cast<int>(); // nums: static type is IEnumerable<int>
Query Syntax
// Use an explicitly typed range variable. var numbers = new object[] { 1, 2, 3, 4, 5 }; var nums = from int n in numbers select n; // nums: static type is IEnumerable<int>
OfType
Filters values, depending on their ability to be cast to a specified type.
Method Syntax
// OfType var objects = new object[] { 1, "one", 2, "two", 3, "three" }; var numbers = objects.OfType<int>(); // nums = { 1, 2, 3 }
Query Syntax
// Not applicable.
ToArray
Converts a collection to an array.
Method Syntax
// ToArray var numbers = Enumerable.Range(1, 5); int[] array = numbers.ToArray(); // array = { 1, 2, 3, 4, 5 }
Query Syntax
// Not applicable.
ToList
Converts a collection to a list.
Method Syntax
// ToList var numbers = Enumerable.Range(1, 5); List<int> list = numbers.ToList(); // list = { 1, 2, 3, 4, 5 }
Query Syntax
// Not applicable.
ToDictionary
Puts elements into a dictionary based on a key selector function.
Method Syntax
// ToDictionary var numbers = new int[] { 1, 2, 3 }; var dict = numbers.ToDictionary(n => n.ToString()); // dict = { "1" => 1, "2" => 2, "3" => 3 }
Query Syntax
// Not applicable.