Linq to Object

来源:互联网 发布:千牛淘宝客服斗图 编辑:程序博客网 时间:2024/05/20 14:24
联接查询,join .. in ... 和 join ... into ... 和group ...  by ...区别
    class Program
    {
        // 申明了一个Student类和一个Score类

        static void Main(string[] args)
        {
            List<Student> stuLst = new List<Student>
            {
                new Student(1,"ycj"),
                new Student(2,"djx"),
                new Student(3,"cbw")
            };
            List<Score> scoreLst = new List<Score>
            {
                new Score(1,"Math",80),
                new Score(1,"English",75),
                new Score(2,"Math",90),
                new Score(2,"English",85),
                new Score(3,"Math",75),
                new Score(3,"English",75)
            };
            /** join in **/
            var query = from stu in stuLst
                        join score in scoreLst
                          on stu.Id equals score.Id
                        select new
                        {
                            Id = stu.Id,
                            Name = stu.Name,
                            Subject = score.Name,
                            Grade = score.Grades
                        };
            foreach (var info in query)
            {
                Console.WriteLine("id:{0}, name:{1}, subject:{2}, score:{3}",
                    info.Id, info.Name, info.Subject, info.Grade);
            }
            Console.WriteLine();
            /** join into **/
            var query2 = from stu in stuLst
                        join score in scoreLst
                          on stu.Id equals score.Id into stuScore
                        select new
                        {
                            Id = stu.Id,
                            Name = stu.Name,
                            Scores = stuScore
                        };
            foreach (var info in query2)
            {
                Console.WriteLine("id:{0}, name:{1}", info.Id, info.Name);
                foreach (var score in info.Scores)
                {
                    Console.WriteLine("subject:{0}, score:{1}", score.Name, score.Grades);
                }
                Console.WriteLine();
            }
            /** group by **/
            var query = from stu in stuLst
                        join score in scoreLst
                          on stu.Id equals score.Id
                        group new
                        {
                            Id = stu.Id,
                            Name = stu.Name,
                            Scores = score.Grades
                        } by stu.Name;
            foreach (var info in query)
            {
                Console.WriteLine(info.Key);
                foreach (var item in info)
                {
                    Console.WriteLine("id:{0}, name:{1}, score:{2}", item.Id, item.Name, item.Scores);
                }
            } 
            Console.ReadKey();
        }
    }

区别:
join ... in:
图片
join ...  into:
图片
group ... by:
图片

PS:
假设有一个大序列A,小序列B,其中A.Length >> B.Length,应该把A作为左边序列,B作为右边序列。因为左边序列的读取是延时的,而右边序列在第一次读取时,它会读取右边序列的全部,来建立一个从建到生成这些键的值的查找。之后,它就不需要再次读取右边序列了,并且开始迭代左边的序列;
啥?你不知道迭代?呵呵~ 关键字: yeild return; 
0 0