当前位置: 移动技术网 > IT编程>开发语言>.net > Linq操作ArrayList

Linq操作ArrayList

2020年03月09日  | 移动技术网IT编程  | 我要评论
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; }
    }
}

 

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网