Linq的学习

Student[] stAry ={
                            new Student("张三","",20),
                            new Student("李四","",21),
                            new Student("王五","",22),
                            new Student("王妈妈","",20),
                            new Student("王丹","",20),
                            new Student("王真","",20),
                            };

数据源 stAry

1、返回数据源中所有元素

 var query = from varl in stAry select varl;
            foreach (var item in query)
            {
                Console.Write("{0}", item);
            }

2.返回数据源中的姓名

var query = from varl in stAry select varl.Name;
foreach (string item in query)
{
    Console.Write("{0}", item);
}

3、返回姓名的长度

var query2 = from varl in stAry select varl.Name.Length;
foreach (var item in query2)
{
    Console.Write("{0}", item);
}

4、查询返回姓名,年龄,姓名长度

var query3 = from varl in stAry select new { varl.Name, varl.Age, varl.Name.Length };
foreach (var item in query3)
{
    Console.Write(item);
}

5、使用 where 指定筛选条件 和 order by 子句进行排序

 int[] ary = { 11, 32, 10, 23, 54, 17, 19 };
            var query = from varl in ary where varl > 15 orderby varl  select varl;


//这里默认是升序ascending,降序是descending

6、使用group by 子句进行分组

 var query9 = from varl in stAry where (varl.Name.StartsWith(""))  group varl by varl.XingBie;


//varl.Name.StartsWith("王") 是Linq里面的模糊查询  =》like ("王%")

 7、用from子句进行复合查询

Student[] stAry ={
                            new Student("张三","",20,new List<LessonScore>{new LessonScore("英语",80.5f),new LessonScore("语文",74.5f),new LessonScore("数学",60.5f)}),
                            new Student("李四","",21,new List<LessonScore>{new LessonScore("英语",60.5f),new LessonScore("语文",75.5f),new LessonScore("数学",80.5f)}),
                            new Student("王五","",22,new List<LessonScore>{new LessonScore("英语",40.5f),new LessonScore("语文",76.5f),new LessonScore("数学",15.5f)}),
                            new Student("王妈妈","",20,new List<LessonScore>{new LessonScore("英语",55.5f),new LessonScore("语文",780.5f),new LessonScore("数学",45.5f)}),
                            new Student("王丹","",20,new List<LessonScore>{new LessonScore("英语",87.5f),new LessonScore("语文",79.5f),new LessonScore("数学",66.5f)}),
                            new Student("王真","",20,new List<LessonScore>{new LessonScore("英语",99.5f),new LessonScore("语文",86.5f),new LessonScore("数学",36.5f)}),
                            };
            //用from子句进行复合查询
            var query = from varl in stAry
                        from j in varl.Scores
                        where j.Score > 80
                        group new { varl.Name, j } by varl.Name;
//group by 的语法是 group element by key  ,其中element表示作为查询结果返回的元素,可以是一个也可以是多个,多个用new{a,b}写法,key表示分组条件
            foreach (var item in query)
            {
                Console.WriteLine("{0}", item.Key);//外层先打印出名字
                foreach (var j in item)
                {
                    System.Console.WriteLine("{0}", j);
                }
            }

在上面这种形式的复合from字句中,内层的from子句并非一定要查询外层from子句中的元素的属性和方法,它可以是任何作为数据源的对象。

8、第二种from复合子句是在多个数据源上进行查询

 //在多个数据源上进行的复合查询
            int[] intAry1 = { 5, 15, 25, 30, 33, 50 };
            int[] intAry2 = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
            var query = from st1 in intAry1
                        from st2 in intAry2
                        where st2 % st1 == 0
                        group st2 by st1;
            foreach (var item in query)
            {
                Console.WriteLine("{0}", item.Key);
                foreach (var item2 in item)
                {
                    Console.WriteLine("{0}", item2);
                }
                Console.WriteLine();
            }

 //理论上可以写无数个from子句,但是会增加代码的复杂度,降低可读性和可维护性,建议不要超过三个

 9、用Join子句进行连接

//用join子句进行连接查询
            //1、内部连接   语法:  join element in dataSource on exp1 equals exp2
            int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
            int[] intAry2 = { 10, 20, 30, 40, 50, 60, 70, 80};
            var query1 = from val1 in intAry1
                         join val2 in intAry2 on val1 % 5 equals val2 % 15
                         select new { VAL1 = val1, VAL2 = val2 };
            foreach (var item in query1)
            {
                Console.WriteLine("{0}", item);
            }


item这里不能点key,因为select 后面接的是多个值

纠正:item这里不能点key, 是group之后才有key

输出的是VAL1%5和VAL2%5结果相等的,查询结果按第一个中的元素优先排列

用join子句进行分组连接

//用join子句进行分组连接
            //语法: join element in dataSource on exp1 equals exp2 into grpName
            //grpName 是保存一组数据的集合
            int[] intAry1 = { 5, 15, 25, 30, 33, 40 };
            int[] intAry2 = { 10, 20, 30, 40, 50, 60, 70, 80 };

            var query = from val1 in intAry1
                        join val2 in intAry2 on val1 % 5 equals val2 % 15 into val2Grp
                        select new { VAL1 = val1, VAL2 = val2Grp };
            foreach (var item in query)
            {
                Console.WriteLine("{0}:", item.VAL1);
                foreach (var item2 in item.VAL2)
                {
                    Console.WriteLine("{0}", item2);
                }
                Console.WriteLine();
            }

//val2Grp存储的是val2%15的结果

33这里为空,是因为intAry1中的元素即使在intAry2中不存在匹配元素也会产生一个空的列表

 

用join子句进行左外部连接

在LINQ中,通过对分组连接的结果调用DefaultIfEmpty()方法来执行外部连接。DefaultIfEmpty()方法从列表中获取置顶元素。如果列表为空则返回默认值。

左外部连接和分组连接相似但是并非一样。分组连接返回的查询结果是一种分层数据结构。需要两层foreach遍历结果。左外部连接是在分组连接的查询结果上再查询,所以在join后还有个from跟进查询。

原文地址:https://www.cnblogs.com/wangcongsuibi/p/8761085.html