English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Conversion operator in LINQ can be used to convert the type of elements in a sequence (collection). Conversion operators are divided into three types:AsOperators (AsEnumerable and AsQueryable),ToOperators (ToArray, ToDictionary, ToList, and ToLookup) andConversionOperators (Cast and OfType).
The following table lists all conversion operators.
Method | Description |
---|---|
AsEnumerable | Return the input sequence as IEnumerable<T> |
AsQueryable | Convert IEnumerable to IQueryable to simulate a remote query provider |
Cast | Convert a non-generic collection to a generic collection (IEnumerable to IEnumerable) |
OfType | Filter the collection based on the specified type |
ToArray | Convert a collection to an array |
ToDictionary | Place elements into Dictionary based on the key selector function |
ToList | Convert a collection to List |
ToLookup | Group elements into Lookup<TKey, TElement> |
The AsEnumerable and AsQueryable methods convert or convert the source object to IEnumerable<T> or IQueryable<T> respectively.
See the following examples:
class Program { static void ReportTypeProperties<T>(T obj) { Console.WriteLine("Compile",-time type: {0} Console.WriteLine("Actual type: {0}", obj.GetType().Name); static void Main(string[] args) { Student[] studentArray = { new Student() { StudentID = 1, StudentName = "John", Age = 18 } new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } new Student() { StudentID = 4, StudentName = "Ram", Age = 20 }, new Student() { StudentID = 5, StudentName = "Ron", Age = 31 } ReportTypeProperties(studentArray); ReportTypeProperties(studentArray.AsEnumerable()); ReportTypeProperties(studentArray.AsQueryable());
Compile-time type: Student[] Actual type: Student[] Compile-time type: IEnumerable`1 Actual type: Student[] Compile-time type: IQueryable`1 Actual type: EnumerableQuery`1
As shown in the above example, the AsEnumerable and AsQueryable methods convert the compile-time type to IEnumerable and IQueryable respectively.
The function of Cast is the same as AsEnumerable<T>. It converts the source object to IEnumerable<T>.
class Program { static void ReportTypeProperties<T>(T obj) { Console.WriteLine("Compile",-time type: {0} Console.WriteLine("Actual type: {0}", obj.GetType().Name); static void Main(string[] args) { Student[] studentArray = { new Student() { StudentID = 1, StudentName = "John", Age = 18 } new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } new Student() { StudentID = 4, StudentName = "Ram", Age = 20 }, new Student() { StudentID = 5, StudentName = "Ron", Age = 31 } ReportTypeProperties(studentArray); ReportTypeProperties(studentArray.Cast<Student>());
Compile-time type: Student[] Actual type: Student[] Compile-time type: IEnumerable`1 Actual type: Student[] Compile-time type: IEnumerable`1 Actual type: Student[] Compile-time type: IEnumerable`1 Actual type: Student[]
studentArray.Cast<Student>() is the same as (IEnumerable<Student>)studentArray, but Cast<Student>() has better readability.
As the name implies, the source object conversions of ToArray(), ToList(), and ToDictionary() methods are respectively an array, a list, or a dictionary.
The To operator enforces the query. It forces the remote query provider to execute the query and retrieve the results from the underlying data source (such as a SQL Server database).
IList<string> strList = new List<string>() { "One", "Two", "Three", "Four", "Three" string[] strArray = strList.ToArray<string>();// Convert list to array IList<string> list = strArray.ToList<string>(); // converts array into list
ToDictionary - Convert generic list to generic dictionary:
IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", age = 18 } new Student() { StudentID = 2, StudentName = "Steve", age = 21 } new Student() { StudentID = 3, StudentName = "Bill", age = 18 } new Student() { StudentID = 4, StudentName = "Ram", age = 20 }, new Student() { StudentID = 5, StudentName = "Ron", age = 21 //The following converts the list to a dictionary, where StudentId is the key IDictionary<int, Student> studentDict = studentList.ToDictionary<Student, int>(s => s.StudentID); foreach(var key in studentDict.Keys) Console.WriteLine("Key: {0}, Value: ",1 key, (studentDict[key] as Student).StudentName);
Key: 1, Value: John Key: 2, Value: Steve Key: 3, Value: Bill Key: 4, Value: Ram Key: 5, Value: Ron
The following figure shows how studentDict contains a key from the above example-value pair, where key is StudentID and value is Student object.