ArrayList實作了System.Collections空間下的IEnumerable介面,這個介面是非泛型的,如果要使用LINQ,必須宣告列舉變數的型別,依賴Cast查詢運算子轉換列舉型別,
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; namespace ConsoleApp4 { class Program { static void Main(string[] args) { var arrayList = GetArrayList(); //查詢運算式 var query = from Student student in arrayList where student.Scores[0] > 95 select student; //方法呼叫 var query2 = arrayList.Cast<Student>().Where(x => x.Scores[0] > 95); } static ArrayList GetArrayList() { ArrayList arrList = new ArrayList { new Student { FirstName = "Svetlana", LastName = "Omelchenko", Scores = new int[] { 98, 92, 81, 60 } }, new Student { FirstName = "Claire", LastName = "O’Donnell", Scores = new int[] { 75, 84, 91, 39 } }, new Student { FirstName = "Claire", LastName = "O’Donnell", Scores = new int[] { 75, 84, 91, 39 } },new Student { FirstName = "Cesar", LastName = "Garcia", Scores = new int[] { 97, 89, 85, 82 } }}; return arrList; } } public class Student { public string FirstName { get; set; } public string LastName { get; set; } public int[] Scores { get; set; } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/69587.html
標籤:其他
上一篇:WebAPI-HTTP編程模型
