عملگرهای تبدیل
در این بخش از آموزش LINQ عملگرهای تبدیل نوع را بررسی خواهیم کرد. همانطور که از اسم آنها نیز مشخص است این عملگرها به منظور تبدیل نوع داده عناصر موجود در یک مجموعه استفاده می شوند. به طور کلی سه نوع عملگر تبدیل داریم که عبارت اند از:
- عملگرهای As (AsEnumerable و AsQueryable)
- عملگرهای To (ToArray، ToDictionary، ToList و ToLookup)
- عملگر Cast
عملگرهای AsEnumerable و AsQueryable
این عملگرها به ترتیب شیء ورودی را به IEnumerable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Program { static void ReportTypeProperties<T>(T obj) { Console.WriteLine("Compile-time type: {0}", typeof(T).Name); 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()); } } |
خروجی مثال:
1 2 3 4 5 6 | Compile-time type: Student[] Actual type: Student[] Compile-time type: IEnumerable`1 Actual type: Student[] Compile-time type: IQueryable`1 Actual type: EnumerableQuery`1 |
عملگرهای ToArray، ToDictionary و ToList
همانطور که از نام این عملگرها مشخص است، شیء ورودی را به آرایه، دیکشنری و لیست تبدیل می کنند. در مثال زیر نحوه استفاده از تو متد ToArray و ToList نشان داده شده است:
1 2 3 4 5 6 7 8 9 | IList<string> strList = new List<string>() { "One", "Two", "Three", "Four", "Three" }; string[] strArray = strList.ToArray<string>();// converts List to Array IList<string> list = strArray.ToList<string>(); // converts array into list |
مثال زیر نحوه استفاده از متد ToDictionary را نشان می دهد:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 } }; //following converts list into dictionary where StudentId is a 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); |
خروجی مثال:
1 2 3 4 5 | Key: 1, Value: John Key: 2, Value: Steve Key: 3, Value: Bill Key: 4, Value: Ram Key: 5, Value: Ron |
عملگر Cast
عملگر Cast همان کار AsEnumerable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Program { static void ReportTypeProperties<T>(T obj) { Console.WriteLine("Compile-time type: {0}", typeof(T).Name); 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>()); } } |
خروجی مثال:
1 2 3 4 5 6 7 8 | 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
هیچ نظری ثبت نشده است