tnblog
首页
视频
资源
登录

EF Group Join简单运用

8830人阅读 2020/6/3 11:02 总访问:688037 评论:0 收藏:0 手机
分类: EF

EF Group Join其实返回的就是一对多的情况,通常用来做有查看详情的,比如我们来实现一个查询学生与考试分数的例子,因为一个学生可以考试很多科目所以可以使用selectMany来实现。


代码很简单:

  1. /// <summary>
  2. /// 查询用户和分数
  3. /// </summary>
  4. /// <returns></returns>
  5. public ActionResult UserScore()
  6. {
  7.     oapEntities oap = new oapEntities();
  8.     List<UserScore> userScore = oap.Users.GroupJoin(oap.Score, a => a.Id, b => b.UsersId, (user, scores) => new UserScore
  9.     {
  10.         user = user,
  11.         scores = scores
  12.     }).ToList();
  13.     return View(userScore);
  14. }

前台,就展示数据即可

  1. @{
  2.     ViewBag.Title = "我的分数";
  3.     Layout = "~/views/_layoutpage.cshtml";
  4. }
  5. <script src="~/Content/js/myplugins.js"></script>
  6. <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
  7. <script src="~/Content/js/jquery.validate.unobtrusive.min.js"></script>
  8. <script src="~/Content/pagebar/js/jqPaginator.js"></script>
  9. <link href="~/Content/pagebar/css/page.css" rel="stylesheet" />
  10. <script src="~/Content/layer/layer.js"></script>
  11. <script>
  12.     $(function ({
  13.         $(".showdetails").click(function ({
  14.             var tds = $(this).parents("tr").find("td");
  15.             layer.open({
  16.                 title"分数详情",
  17.                 content: tds.eq(5).html()
  18.             });
  19.         });
  20.     });
  21. </script>
  22. @using EFLearn2.Models
  23. @model List<UserScore>
  24. <div class="row-fluid">
  25.     <form method="post" id="searchform" action="/home/index">
  26.         <input type="text" placeholder="请输入用户名" name="username" value="@ViewBag.username" />
  27.         <input type="text" placeholder="请输入学号" name="number" value="@ViewBag.number" />
  28.         <input type="hidden" name="page" id="pagehidden" />
  29.         <select id="checktypes" name="checktype">
  30.             <option value="-1">请选择审核状态</option>
  31.             <option value="1">通过</option>
  32.             <option value="2">审核中</option>
  33.             <option value="3">审核失败</option>
  34.         </select>
  35.         <button class="btn" style="margin-top: -10px; padding: 5px 10px" id="search">查询</button>
  36.     </form>
  37. </div>
  38. <div>
  39.     <a href="#" class="btn mini batdeletea" style="background-color: #ff6666; color: #fff"><i class="icon-trash"></i>删除</a>
  40.     <a href="#" class="btn mini batupdate" style="background-color: #aaaaff; color: #fff"><i class="icon-star"></i>保存</a>
  41.     <a href="#" class="btn mini batchcancel" style="background-color: #aacc66; color: #fff"><i class="icon-share"></i>撤销</a>
  42.     <a href="#" class="btn mini adda" style="background-color: #ff99ff; color: #fff"><i class="icon-plus"></i>添加</a>
  43.     <a href="#" class="btn mini batchedit" style="background-color: #aaaaff; color: #fff"><i class="icon-star"></i>编辑</a>
  44. </div>
  45. <div class="portlet-body" style="margin-top: 3px">
  46.     <table id="userTable" class="table table-striped table-bordered table-advance table-hover ">
  47.         <thead>
  48.             <tr>
  49.                 <th>
  50.                     <input type="checkbox" id="headcheck" />
  51.                 </th>
  52.                 <th><i class="icon-briefcase"></i>编号</th>
  53.                 <th><i class="icon-briefcase"></i>姓名</th>
  54.                 <th class="hidden-phone"><i class="icon-user"></i>学号</th>
  55.                 <th><i class="icon-shopping-cart"></i>班级</th>
  56.                 <th><i class="icon-shopping-cart"></i>操作</th>
  57.             </tr>
  58.         </thead>
  59.         <tbody>
  60.             @foreach (UserScore item in Model)
  61.             {
  62.                 <tr>
  63.                     <td>
  64.                         <input type="checkbox" value="@item.user.Id" />
  65.                     </td>
  66.                     <td>@item.user.Id</td>
  67.                     <td>@item.user.UserName</td>
  68.                     <td>@item.user.Number</td>
  69.                     <td>@item.user.UClass</td>
  70.                     <td style="display: none">
  71.                         @foreach (var score in item.scores)
  72.                         {
  73.                             <div>@score.Sub:@score.Score1</div>
  74.                         }
  75.                     </td>
  76.                     <td style="width: 109px">
  77.                         <a href="#" class="showdetails">分数</a>
  78.                         <a href="#" class="">宠物</a>
  79.                         <a href="#" class="deletea">删除</a>
  80.                     </td>
  81.                 </tr>
  82.             }
  83.         </tbody>
  84.     </table>
  85.     <ul class="pagination" id="pagination" style="margin-top: 10px; float: right"></ul>
  86. </div>

先用一个隐藏域放着在用layer弹出来就行了,效果如下:



当然我们可以来更多表的group join比如来个4表的

  1. public ActionResult UserScore()
  2. {
  3.     oapEntities oap = new oapEntities();
  4.     //四表的group join
  5.     List<UserScore> userScore = oap.Users.GroupJoin(oap.Score, a => a.Id, b => b.UsersId, (user, scores) => new
  6.     {
  7.         user = user,
  8.         scores = scores
  9.     }).GroupJoin(oap.Pet, a => a.user.Id, b => b.UserId, (before, pets) => new
  10.     {
  11.         user = before.user,
  12.         scores = before.scores,
  13.         pets = pets
  14.     }).GroupJoin(oap.User_Parent, a => a.user.Id, b => b.UsersId, (a, parents) => new UserScore
  15.     {
  16.         user = a.user,
  17.         pets = a.pets,
  18.         scores = a.scores,
  19.         parents = parents
  20.     }).ToList();
  21.     return View(userScore);
  22. }

前台一样的只是可以一次展示更多数据而已

  1. @{
  2.     ViewBag.Title = "我的分数";
  3.     Layout = "~/views/_layoutpage.cshtml";
  4. }
  5. <script src="~/Content/js/myplugins.js"></script>
  6. <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
  7. <script src="~/Content/js/jquery.validate.unobtrusive.min.js"></script>
  8. <script src="~/Content/pagebar/js/jqPaginator.js"></script>
  9. <link href="~/Content/pagebar/css/page.css" rel="stylesheet" />
  10. <script src="~/Content/layer/layer.js"></script>
  11. <script>
  12.     $(function ({
  13.         $(".showdetails").click(function ({
  14.             var tds = $(this).parents("tr").find("td");
  15.             layer.open({
  16.                 title"分数详情",
  17.                 content: tds.eq(5).html()
  18.             });
  19.         });
  20.         $(".showpet").click(function ({
  21.             var tds = $(this).parents("tr").find("td");
  22.             layer.open({
  23.                 title"宠物详情",
  24.                 content: tds.eq(6).html()
  25.             });
  26.         });
  27.         $(".showparents").click(function ({
  28.             var tds = $(this).parents("tr").find("td");
  29.             layer.open({
  30.                 title"家长详情",
  31.                 content: tds.eq(7).html()
  32.             });
  33.         });
  34.     });
  35. </script>
  36. @using EFLearn2.Models
  37. @model List<UserScore>
  38. <div class="row-fluid">
  39.     <form method="post" id="searchform" action="/home/index">
  40.         <input type="text" placeholder="请输入用户名" name="username" value="@ViewBag.username" />
  41.         <input type="text" placeholder="请输入学号" name="number" value="@ViewBag.number" />
  42.         <input type="hidden" name="page" id="pagehidden" />
  43.         <select id="checktypes" name="checktype">
  44.             <option value="-1">请选择审核状态</option>
  45.             <option value="1">通过</option>
  46.             <option value="2">审核中</option>
  47.             <option value="3">审核失败</option>
  48.         </select>
  49.         <button class="btn" style="margin-top: -10px; padding: 5px 10px" id="search">查询</button>
  50.     </form>
  51. </div>
  52. <div>
  53.     <a href="#" class="btn mini batdeletea" style="background-color: #ff6666; color: #fff"><i class="icon-trash"></i>删除</a>
  54.     <a href="#" class="btn mini batupdate" style="background-color: #aaaaff; color: #fff"><i class="icon-star"></i>保存</a>
  55.     <a href="#" class="btn mini batchcancel" style="background-color: #aacc66; color: #fff"><i class="icon-share"></i>撤销</a>
  56.     <a href="#" class="btn mini adda" style="background-color: #ff99ff; color: #fff"><i class="icon-plus"></i>添加</a>
  57.     <a href="#" class="btn mini batchedit" style="background-color: #aaaaff; color: #fff"><i class="icon-star"></i>编辑</a>
  58. </div>
  59. <div class="portlet-body" style="margin-top: 3px">
  60.     <table id="userTable" class="table table-striped table-bordered table-advance table-hover ">
  61.         <thead>
  62.             <tr>
  63.                 <th>
  64.                     <input type="checkbox" id="headcheck" />
  65.                 </th>
  66.                 <th><i class="icon-briefcase"></i>编号</th>
  67.                 <th><i class="icon-briefcase"></i>姓名</th>
  68.                 <th class="hidden-phone"><i class="icon-user"></i>学号</th>
  69.                 <th><i class="icon-shopping-cart"></i>班级</th>
  70.                 <th><i class="icon-shopping-cart"></i>操作</th>
  71.             </tr>
  72.         </thead>
  73.         <tbody>
  74.             @foreach (UserScore item in Model)
  75.             {
  76.                 <tr>
  77.                     <td>
  78.                         <input type="checkbox" value="@item.user.Id" />
  79.                     </td>
  80.                     <td>@item.user.Id</td>
  81.                     <td>@item.user.UserName</td>
  82.                     <td>@item.user.Number</td>
  83.                     <td>@item.user.UClass</td>
  84.                     <td style="display: none">
  85.                         @foreach (var score in item.scores)
  86.                         {
  87.                             <div>@score.Sub:@score.Score1</div>
  88.                         }
  89.                     </td>
  90.                     <td style="display: none">
  91.                         @foreach (var pet in item.pets)
  92.                         {
  93.                             <div>@pet.CatName:@pet.DogName</div>
  94.                         }
  95.                     </td>
  96.                     <td style="display: none">
  97.                         @foreach (var parent in item.parents)
  98.                         {
  99.                             <div>@parent.Father:@parent.Mother</div>
  100.                         }
  101.                     </td>
  102.                     <td style="width: 139px">
  103.                         <a href="#" class="showdetails">分数</a>
  104.                         <a href="#" class="showpet">宠物</a>
  105.                         <a href="#" class="showparents">家长</a>
  106.                         <a href="#" class="deletea">删除</a>
  107.                     </td>
  108.                 </tr>
  109.             }
  110.         </tbody>
  111.     </table>
  112.     <ul class="pagination" id="pagination" style="margin-top: 10px; float: right"></ul>
  113. </div>




欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

css定位的简单运用

父容器使用相对定位position: relative子容器使用绝对定位position: relative这样就可以子容器相对父容器定位了,可以写一...

.netcore3.1 RabbitMq 简单运用与相关方法的介绍

.netcore3.1 RabbitMq 简单运用与相关方法的介绍[TOC] 在这里我将使用简单的生产—-&gt;加入队列—-&gt;消费,做一个简...

Jenkins简单运用

Jenkins简单运用[TOC] 环境预备如果没有安装Jenkins,在此之前大家可以按照如下两篇文章进行安装:docker安装JenkinsJenki...

Jenkins Pipeline简单运用

Jenkins Pipeline简单运用[TOC] Pipeline简单运用创建一个任务流水线 我们从中可以看出,他最大的不同就是多了一个Pi...

Kubernetes AppArmor 简单运用

Kubernetes AppArmor 简单运用[TOC] AppArmor 简介AppArmor 是一个有效且易于使用的 Linux 应用程序安全系统。AppArmor 通...

安装 Windbg 简单运用

安装 Windbg 简单运用[TOC] 软件安装列表1.Visual Studio 2022: 安装 .NET Framework 4.8, .NET 6.0.5 ,支持 C++ 模板2....

Multus-CNI与whereabouts的简单运用

Multus-CNI[TOC] Multus-CNI 简介简单来讲,这玩意可以对一个pod插入多张网卡进行通信。同时也支持多种cni的插件,什么Fla...
如果有缘,错过了还会重来,如果无缘,相遇了也会离开
排名
9
文章
115
粉丝
5
评论
5
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术