码迷,mamicode.com
首页 > 数据库 > 详细

Linq to SQL 练习

时间:2018-10-17 17:46:14      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:home   list()   dna   foreach   his   distinct   list   lis   each   

  1     public class HomeController : Controller
  2     {
  3         //
  4         // GET: /Home/
  5         empentity entity = new empentity();
  6         public ActionResult Index()
  7         {
  8             List<EmpCount> lst = (from m in entity.emp.ToList()
  9                                   join n in entity.dept.ToList() on m.DEPTNO equals n.DEPTNO
 10                                   group m by m.DEPTNO into newEmp
 11                                   select new EmpCount
 12                                   {
 13                                       DeptID = newEmp.Key,
 14                                       DName = entity.dept.Where(p => p.DEPTNO == newEmp.Key).FirstOrDefault().DNAME,
 15                                       Count = newEmp.Count()
 16                                   }).ToList();
 17                                
 18                                 
 19             return View(lst);
 20         }
 21         public ActionResult SelSal()
 22         {
 23             List<emp> lst = (from s in entity.emp.ToList()
 24                             select s).ToList();
 25             return View(lst);
 26         }
 27         public ActionResult SelEmpSal()
 28         {
 29             ViewBag.SalgradeId = new SelectList(entity.salgrade.ToList(), "SalgradeId", "GRADE");
 30             return View(entity.emp.ToList());
 31         }
 32         [HttpPost]
 33         public ActionResult SelEmpSal(int SalgradeId)
 34         {
 35             ViewBag.SalgradeId = new SelectList(entity.salgrade.ToList(), "SalgradeId", "GRADE");
 36             Salgrade lst = (from s in entity.salgrade.ToList()
 37                     where s.SalgradeId == SalgradeId
 38                     select s).FirstOrDefault();
 39             int Max = lst.HISAL;
 40             int Min = lst.LOSAL;
 41             List<emp> lst1 = (from m in entity.emp.ToList()
 42                              where m.SAL > Min && m.SAL < Max
 43                              select m).ToList();
 44             return View("SelEmpSal", lst1);
 45 
 46         }
 47         public ActionResult EmpSal1()
 48         {
 49             Salgrade sal = (from s in entity.salgrade.ToList()
 50                            where s.GRADE.Contains("二级")
 51                            select s).FirstOrDefault();
 52             int Max = sal.HISAL;
 53             int Min = sal.LOSAL;
 54             List<emp> lst = (from i in entity.emp.ToList()
 55                             where i.SAL > Min && i.SAL < Max
 56                             select i).ToList();
 57             return View(lst);
 58         }
 59         public ActionResult EmpCount()
 60         { 
 61             Salgrade sal = (from s in entity.salgrade.ToList()
 62                            where s.GRADE.Contains("一级")
 63                            select s).FirstOrDefault();
 64             int Max = sal.HISAL;
 65             int Min = sal.LOSAL;
 66             List<EmpCount> count = (from m in entity.emp.ToList()
 67                                     join n in entity.dept.ToList() on m.DEPTNO equals n.DEPTNO
 68                                     where m.SAL > Min && m.SAL < Max
 69                                     group m by m.DEPTNO into newEmp
 70                                     select new EmpCount
 71                                     {
 72                                         DeptID = newEmp.Key,
 73                                         DName = entity.dept.Where(p => p.DEPTNO == newEmp.Key).FirstOrDefault().DNAME,
 74                                         Count = newEmp.Count()
 75                                     }).ToList();
 76             return View(count);
 77         }
 78         public ActionResult EmpSal2()
 79         {
 80             Salgrade sal = (from s in entity.salgrade.ToList()
 81                             where s.GRADE == "二级"
 82                             select s).FirstOrDefault();
 83             int Max = sal.HISAL;
 84             int Min = sal.LOSAL;
 85             List<emp> lst = (from i in entity.emp.ToList()
 86                              where i.SAL > Min && i.SAL < Max
 87                              select i).ToList();
 88             return View(lst);
 89         }
 90         public ActionResult EmpCount1()
 91         {
 92             Salgrade sal = (from s in entity.salgrade.ToList()
 93                             where s.GRADE == "一级"
 94                             select s).FirstOrDefault();
 95             int Max = sal.HISAL;
 96             int Min = sal.LOSAL;
 97             List<EmpCount> lst = (from m in entity.emp.ToList()
 98                                   join n in entity.dept.ToList() on m.DEPTNO equals n.DEPTNO
 99                                   where m.SAL>Min&&m.SAL<Max
100                                   group m by m.DEPTNO into newEmp
101                                   select new EmpCount
102                                   {
103                                       DeptID = newEmp.Key,
104                                       DName = entity.dept.Where(p => p.DEPTNO == newEmp.Key).FirstOrDefault().DNAME,
105                                       Count = newEmp.Count()
106                                   }).ToList();
107             return View(lst);
108         }
109         public ActionResult EmpLd()
110         {
111             List<emp> lst = (from n in entity.emp.ToList()
112                             join m in entity.emp.ToList() on n.EMPNO equals m.MGR
113                              select n).Distinct().ToList();
114             ViewBag.DEPTNO = new SelectList(entity.dept.ToList(), "DEPTNO", "DNAME");
115             ViewBag.SalgradeId = new SelectList(entity.salgrade.ToList(), "SalgradeId", "GRADE");
116             return View(lst);
117         }
118         [HttpPost]
119         public ActionResult EmpLd1(int DEPTNO, int SalgradeId)
120         {
121             ViewBag.DEPTNO = new SelectList(entity.dept.ToList(), "DEPTNO", "DNAME");
122             ViewBag.SalgradeId = new SelectList(entity.salgrade.ToList(), "SalgradeId", "GRADE");
123             Salgrade sal = entity.salgrade.Find(SalgradeId);
124             int Max = sal.HISAL;
125             int Min = sal.LOSAL;
126             List<emp> lst = (from n in entity.emp.ToList()
127                              join m in entity.emp.ToList() on n.EMPNO equals m.MGR
128                              where n.SAL > Min && n.SAL < Max && n.DEPTNO == DEPTNO
129                              select n).Distinct().ToList();
130             foreach (emp item in lst)
131             {
132                 emp s = entity.emp.Find(item.EMPNO);
133                 s.SAL = item.SAL + 2000;
134             }
135             entity.SaveChanges();
136             return RedirectToAction("EmpLd");
137         }
138         public ActionResult EmpLd2()
139         {
140             List<emp> ld = (from m in entity.emp.ToList()
141                             join n in entity.emp.ToList() on m.EMPNO equals n.MGR
142                             select m).Distinct().ToList();
143             ViewBag.DEPTNO = new SelectList(entity.dept.ToList(), "DEPTNO", "DNAME");
144             ViewBag.SalgradeId = new SelectList(entity.salgrade.ToList(), "SalgradeId", "GRADE");
145             return View(ld);
146         }
147         [HttpPost]
148         public ActionResult EmpLd3(int DEPTNO, int SalgradeId)
149         {
150             ViewBag.DEPTNO = new SelectList(entity.dept.ToList(), "DEPTNO", "DNAME");
151             ViewBag.SalgradeId = new SelectList(entity.salgrade.ToList(), "SalgradeId", "GRADE");
152             Salgrade sal = entity.salgrade.Find(SalgradeId);
153             List<emp> lst = (from n in entity.emp.ToList()
154                             join m in entity.emp.ToList() on n.EMPNO equals m.MGR
155                             where n.SAL > sal.LOSAL && n.SAL < sal.HISAL && n.DEPTNO == DEPTNO
156                             select n).Distinct().ToList();
157             foreach (emp item in lst)
158             {
159                 emp s = entity.emp.Find(item.EMPNO);
160                 s.SAL = item.SAL + 2000;
161             }
162             entity.SaveChanges();
163             return RedirectToAction("EmpLd2");
164         }
165     }

 

Linq to SQL 练习

标签:home   list()   dna   foreach   his   distinct   list   lis   each   

原文地址:https://www.cnblogs.com/DataBase-123/p/9805362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!