欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linq操作ArrayList

程序员文章站 2023-11-13 16:50:10
ArrayList实现了System.Collections空间下的IEnumerable接口,这个接口是非泛型的。如果要使用LINQ,必须声明枚举变量的类型,依赖Cast查询运算符转换枚举类型。 using System; using System.Collections; using Syste ......

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