pkm的linq to Entities学习2

简介: using (testEntities TestEntity = new testEntities()) { #region linq to entities 内...
            using (testEntities TestEntity = new testEntities())
            {
                #region linq to entities 内容练习2

                #region 父子关系表查询
                /*
                var maxScorePerClass = from s in TestEntity.stuinfo
                                       group s by s.classID into s1
                                       select new
                                       {
                                           classid = s1.Key,
                                           stuinfo = from s2 in s1
                                                     where s2.score == s1.Max(p => p.score)
                                                     select s2
                                       };
                foreach (var sc in maxScorePerClass)  //stuinfo 为 IEnumerable<stuinfo> 类型
                {
                    Console.WriteLine("classID:{0}", sc.classid);
                    Console.WriteLine("每班最高分数:{0}", sc.stuinfo.First().score);
                }
                Console.WriteLine();
                 */
                #endregion

                #region 查询每班低于平均成绩的学生
                /*
                var LitThanAvgScorePerClass = from s in TestEntity.stuinfo
                                              group s by s.classID into s1
                                              select new
                                              {
                                                  classid = s1.Key,
                                                  stuinfo = from s2 in s1
                                                            where s2.score <= s1.Average(p => p.score)//修改此处,可查每班最低成绩、最高成绩等
                                                            select s2
                                              };
                //     Console.Clear();
                foreach (var sc in LitThanAvgScorePerClass)  //stuinfo 为 IEnumerable<stuinfo> 类型
                {
                    Console.WriteLine("每班低于平均分数-classID:{0}", sc.classid);
                    foreach (var stu in sc.stuinfo)
                    {
                        Console.WriteLine("分数:{0}", stu.score);
                    }
                }
                Console.WriteLine();
                 */
                #endregion

                #region 模糊查询---相当于like
                /*
                var querylike = from s in TestEntity.stuinfo
                                where s.username.Contains("pkm")  //相当于like '%pkm%'
                                select s;
                foreach (var s in querylike)
                {
                    Console.WriteLine(s.username);
                }
                Console.WriteLine();
                 */
                #endregion

                #region 编译查询--类似于函数
                /*
                var function = CompiledQuery.Compile((testEntities edm1, string user)//参数
                            => from s in edm1.stuinfo
                               where s.username == user
                               select s
                            );
                var stu001 = function(TestEntity, "pkm001");//调用编译查询
                foreach (var s in stu001)
                {
                    Console.WriteLine(s.username);
                }
                Console.WriteLine();
                 */
                #endregion

                #region any查询,相当于 in
                /*
                IQueryable<long> classIDs = TestEntity.classinfo.Select(c => c.classID);
                var inValues = from s in TestEntity.stuinfo
                               where classIDs.Any(id => id == s.classID)
                               select s;
                foreach (var s in inValues)
                {
                    Console.WriteLine(s.username);
                }
                Console.WriteLine(); 
                 */
                #endregion

                #region 插入数据--相当于insert
                /*
                var newStudent = new stuinfo()
                        {
                            username = "test0003",
                            userpwd = "test0003",
                            score = 95,
                            age = 30,
                            classID = 2,
                            CreateTime = DateTime.Now
                        };
                TestEntity.AddTostuinfo(newStudent);
                //TestEntity.AddObject("stuinfo", newStudent);  //这句的上面一句是一样的
                TestEntity.SaveChanges();
                Console.Write("添加数据成功!"); 
                */
                #endregion

                #region 修改数据 -- update
                /*
                var toModify = (from s in TestEntity.stuinfo
                                where s.id == 9
                                select s).FirstOrDefault();
                toModify.userpwd = "test";
                TestEntity.SaveChanges(); //修改之后进行保存就行了。 
                 */
                #endregion

                #region 删除数据 delete
                /*
                var toDelete = (from s in TestEntity.stuinfo
                                where s.id == 10
                                select s).FirstOrDefault();
                TestEntity.DeleteObject(toDelete);
                TestEntity.SaveChanges(); 
                 */
                #endregion
                #region 删除前不查询,只指定主键的值
                  var deleteStu = new stuinfo
                {
                id = 9
                };
                MyEntity.AttachTo("stuinfo", deleteStu);
                MyEntity.DeleteObject(deleteStu);
                MyEntity.SaveChanges();//如果有此主键则删除,否则将出异常。要使用try…catch…包含
                  #endregion
                #region 查询结果转换为 List,Array,Dictionary 
                var tempstudents = from s in TestEntity.stuinfo
                                   where s.CreateTime.Value.Year == 2012
                                   select s;
                //stuinfo[] array = tempstudents.ToArray();
                //List<stuinfo> list = tempstudents.ToList();
                Dictionary<long, stuinfo> dict = tempstudents.ToDictionary(s => s.id);
                foreach (var s in dict.Values)
                {
                    Console.WriteLine("用户:{0},密码:{1}", s.username, s.userpwd);
                }
                Console.WriteLine(); 
                #endregion
                

                #endregion
            }

相关文章
|
3月前
|
SQL 开发框架 .NET
聊聊 System.Linq.Dynamic,以及分享一个使用 System.Linq.Dynamic 扩展 LINQ 查询的详细例子
聊聊 System.Linq.Dynamic,以及分享一个使用 System.Linq.Dynamic 扩展 LINQ 查询的详细例子
117 0
|
.NET 开发框架 数据库
深入调研Linq to Objects Join Linq to Entity
最近工作中遇到数据库组合查询带来的一些问题,因此有必要调研一下Linq to Objects Join Linq to Entity。参考一些网友的代码案例,深入实践了一下使用EntityFramework Code First 下的组合查询。
1251 0
|
SQL .NET C#