Linq操作ArrayList


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; }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM