码迷,mamicode.com
首页 > 其他好文 > 详细

Linq操作ArrayList

时间:2020-03-06 01:22:46      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:class   pre   lis   arc   变量   names   ons   namespace   EAP   

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

 

 

Linq操作ArrayList

标签:class   pre   lis   arc   变量   names   ons   namespace   EAP   

原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/12424058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!