LINQ中的Conversion運(yùn)算符可用于轉(zhuǎn)換序列(集合)中元素的類型。轉(zhuǎn)換運(yùn)算符分為三種:As運(yùn)算符(AsEnumerable和AsQueryable),To運(yùn)算符(ToArray,ToDictionary,ToList和ToLookup)和轉(zhuǎn)換運(yùn)算符(Cast和OfType)。
下表列出了所有轉(zhuǎn)換運(yùn)算符。
方法 | 描述 |
---|---|
AsEnumerable | 將輸入序列作為 IEnumerable < T> 返回 |
AsQueryable | 將IEnumerableto轉(zhuǎn)換為IQueryable,以模擬遠(yuǎn)程查詢提供程序 |
Cast | 將非泛型集合轉(zhuǎn)換為泛型集合(IEnumerable到IEnumerable) |
OfType | 基于指定類型篩選集合 |
ToArray | 將集合轉(zhuǎn)換為數(shù)組 |
ToDictionary | 根據(jù)鍵選擇器函數(shù)將元素放入 Dictionary 中 |
ToList | 將集合轉(zhuǎn)換為 List |
ToLookup | 將元素分組到 Lookup<TKey,TElement> |
AsEnumerable和AsQueryable方法分別將源對象轉(zhuǎn)換或轉(zhuǎn)換為IEnumerable <T>或IQueryable <T>。
請看以下示例:
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()); } }
Compile-time type: Student[] Actual type: Student[] Compile-time type: IEnumerable`1 Actual type: Student[] Compile-time type: IQueryable`1 Actual type: EnumerableQuery`1
如上例所示,AsEnumerable和AsQueryable方法分別將編譯時間類型轉(zhuǎn)換為IEnumerable和IQueryable
Cast的作用與AsEnumerable<T>相同。它將源對象強(qiáng)制轉(zhuǎn)換為IEnumerable<T>。
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>()); } }
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>() 與 (IEnumerable<Student>)studentArray 相同,但是 Cast<Student>() 可讀性更好。
顧名思義,ToArray(),ToList(),ToDictionary()方法的源對象轉(zhuǎn)換分別為一個數(shù)組,列表或字典。
To 運(yùn)算符強(qiáng)制執(zhí)行查詢。它強(qiáng)制遠(yuǎn)程查詢提供者執(zhí)行查詢并從底層數(shù)據(jù)源(如SQL Server數(shù)據(jù)庫)獲取結(jié)果。
IList<string> strList = new List<string>() { "One", "Two", "Three", "Four", "Three" }; string[] strArray = strList.ToArray<string>();// 將列表轉(zhuǎn)換為數(shù)組 IList<string> list = strArray.ToList<string>(); // converts array into list
ToDictionary - 將泛型列表轉(zhuǎn)換為泛型詞典:
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 } }; //以下將列表轉(zhuǎn)換成字典,其中StudentId是鍵 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
下圖顯示了上面示例中的studentDict如何包含一個key-value對,其中key是StudentID,value是Student對象。