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

LinQ查询基础(一)LinQ查询表达式

程序员文章站 2022-04-15 14:12:52
...

1.1 用from子句指定数据源
from localVar in dataSource

一般情况下,编译器会根据数据源类型自动为from子句的localVar元素制定数据类型

当然也可自己指定数据类型,编译器不会检查数据类型,也就是当数据类型错误的时候,编译器不会报错,只有在查询结果被使用的时候,才会进行检查从而报错。
            int[] ary = { 1, 2, 3, 5, 7, 9 };
            var query =
                from v in ary
                select v;
输出结果为:
LinQ查询基础(一)LinQ查询表达式


1.2 用select子句指定目标数据
            Student[] arry =
            {
                new Student("张三",20,"男"),
            new Student("李四", 22, "男"),
            new Student("王五", 21, "女"),
            new Student("赵六", 21, "女")        };

            var query =
                from v in arry
                select new { v.Name,v.Age,v.Gender};

            foreach (var v in query)
                Console.WriteLine("{0} ", v);
输出结果:
LinQ查询基础(一)LinQ查询表达式

1.3 用where子句指定筛选条件
            Student[] arry =
            {
                new Student("张三",20,"男"),
            new Student("李四", 22, "男"),
            new Student("王五", 21, "女"),
            new Student("赵六", 21, "女")        };

            var query =
                from v in arry
                where v.Age > 20
                where v.Gender == "男"
                select new { v.Name, v.Age, v.Gender };

            foreach (var v in query)
                Console.WriteLine("{0} ", v);

可以使用多个并列的where子句来进行条件过滤
输出结果:
LinQ查询基础(一)LinQ查询表达式

1.4 用orderby 子句排序(ascending 升序  descending 降序)
Student[] arry =
            {
                new Student("张三",20,"男"),
            new Student("李四", 22, "男"),
            new Student("王五", 21, "女"),
            new Student("赵六", 21, "女")        };

            var query =
                from v in arry
                orderby v.Name.Length ascending, v.Age descending 
                select new { v.Name, v.Age, v.Gender };

            foreach (var v in query)
                Console.WriteLine("{0} ", v);

当一个LinQ查询中出现多个orderby的时候,只有最后一句有效
输出结果:
LinQ查询基础(一)LinQ查询表达式

1.5用group子句进行分组
Student[] arry =
            {
                new Student("张三",20,"男"),
            new Student("李四", 22, "男"),
            new Student("王五", 21, "女"),
            new Student("赵六", 21, "女")        };

            var query =
                from v in arry
                group v by v.Gender;

            foreach (var v in query)
            {
                Console.WriteLine("{0} ", v.Key);
                foreach (var x in v)
                {
                    Console.WriteLine("\t{0}-{1}-{2}", x.Name, x.Age, x.Gender);
                }
            }

输出结果:
LinQ查询基础(一)LinQ查询表达式

1.6 用from子句进行复合查询
    class Program
    {

        class Lessn
        {
            public string Name { get; set; }
            public int Score { get; set; }
            public Lessn(string name, int s)
            {
                Name = name;
                Score = s;
            }
        }

        class Student
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Gender { get; set; }
            public List<Lessn> Scores = new List<Lessn>();
            public Student(string name, int age, string gender, params Lessn[] lessn)
            {
                Name = name;
                Age = age;
                Gender = gender;
                foreach (var x in lessn)
                    Scores.Add(x);
            }
        }

        static void Main(string[] args)
        {
            Student[] arry =
            {
                new Student("张三",20,"男",new Lessn("数学",81),new Lessn("英语",70),new Lessn("语文",60)),
            new Student("李四", 22, "男",new Lessn("数学",90),new Lessn("英语",80),new Lessn("语文",50)),
            new Student("王五", 21, "女",new Lessn("数学",81),new Lessn("英语",55),new Lessn("语文",50)),
            new Student("赵六", 21, "女",new Lessn("数学",50),new Lessn("英语",70),new Lessn("语文",40))        };

            var query =
                from v in arry
                from v2 in v.Scores
                group new { v.Name, v2 } by v.Name;

            foreach (var v in query)
            {
                Console.WriteLine("{0} ", v.Key);
                foreach (var x in v)
                {
                    Console.WriteLine("\tName = {0},  scr:{1} == {2}分", x.Name, x.v2.Name, x.v2.Score);
                }
            }

            Console.ReadKey();
        }
    }
输出结果:
LinQ查询基础(一)LinQ查询表达式
1.7用join子句进行联接

内部联接

join element in dataSource on exp1 equals exp2

分组联接
join element in dataSource on exp1 equals exp2 into graName

左外部连接比较复杂 就实例说明
            int[] arry1 = { 5, 15, 23 };
            int[] arry2 = { 10, 20, 30, 50, 70, 90 };
            //通过对分组联接的结果调用DefaultIfEmpty()来执行左外部联接
            //DefaultIfEmpty()方法从列表中获取指定元素,如果列表为空,返回默认值。
            var query =
                from v in arry1
                join v2 in arry2 on v % 5 equals v2 % 5 into v3
                from v4 in v3.DefaultIfEmpty()
                select new { V = v, V2 = v4 };

            foreach(var v in query)
            {
                Console.WriteLine("{0}", v);
            }

输出结果:    
LinQ查询基础(一)LinQ查询表达式


相关标签: LINQ