c#LINQ使用take和skip加入

时间:2015-03-17 06:16:47

标签: c# jquery asp.net linq

我正在尝试使用ASP MVC和jquery datatables API生成员工DTR。

继承我的控制器代码:

public ActionResult ajaxHandler(jQueryDataTableParamModel param)
    {
        var PeriodDates = db.tTAPeriods.Single(x => x.PeriodID == param.CustomParam_PeriodID);


         IEnumerable<DatatablesViewModel> viewmodel = (from a in db.tHREmployees
                  join b in db.tTADTRs on a.EmpID equals b.EmpID
                  join c in db.tHRJobGrades on a.JobGrdID equals c.JobGrdID
                  join d in db.tTAShifts on b.ShftID equals d.ShftID
                  where (b.LogDt >= PeriodDates.FromDt && b.LogDt <= PeriodDates.ToDt)
                  select new DatatablesViewModel { 
                      EmpID = a.EmpID, 
                      LastNm = a.LastNm, 
                      LogDT = b.LogDt, 
                      LogIn = b.LogIn, 
                      LogOut = b.LogOut, 
                      JobGradeDesc = c.JobGrdDesc, 
                      ShftNm = d.ShftNm 
                  });

        //IF USER SELECTED ALL
         if (param.CustomParam_EmpID != "all")
         {
             viewmodel = viewmodel.Where(x => x.EmpID == param.CustomParam_EmpID);
         }

        //SELECT PAGE FILDTER
         var rslt = from x in viewmodel.Skip(param.iDisplayStart).Take(param.iDisplayLength)
                    select new string[] {
                        x.EmpID,x.LastNm,
                        x.LogDT.ToShortDateString(),
                        x.LogIn.ToString(),
                        x.LogOut.ToString(),
                        x.JobGradeDesc,x.ShftNm
                    };




        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = viewmodel.Count(),
            iTotalDisplayRecords = viewmodel.Count(),
            aaData = rslt.ToArray()
        },
     JsonRequestBehavior.AllowGet);


    }

查看

<div class="form-control">
        @Html.DropDownList("PeriodID", "Choose...")
        @Html.DropDownList("SelectedEmpID", "Choose...")
</div>


<table class="display" id="datatable">
    <thead>

        <tr>

            <th>EmpID</th>
            <th>LastNm</th>
            <th>LogDT</th>
            <th>Log in</th>
            <th>Log Out</th>
            <th>JobGrade</th> 
            <th>Shift</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>EmpID</th>
            <th>LastNm</th>
            <th>LogDT</th>
            <th>Log in</th>
            <th>Log Out</th>
            <th>JobGrade</th>
            <th>Shift</th>
        </tr>
    </tfoot>
    <tbody></tbody>
</table>

@section scripts{
    <!-- DataTables -->
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.js"></script>




    <script>

        function ReInitializeDatatable() {
            $('#datatable').dataTable({
                "bServerSide": true,
                "sAjaxSource": '@Url.Action("ajaxHandler", "datatablesDTR")',
                "bProcessing": true,
                "bDestroy": true, // TO AVOID ERROR RE INITIALIZE
                "aoColumns": [

                                { "sName": "EmpID" },
                                { "sName": "LastNm" },
                                { "sName": "LogDT" },
                                { "sName": "LogIn" },
                                { "sName": "LogOut" },
                                { "sName": "JobGradeDesc" },
                                { "sName": "ShftNm" }

                ],
                // PUSH SEND ADDITIONAL PARAMETER DATA :d
                "fnServerParams": function (aoData) {
                    aoData.push(
                        { "name": "CustomParam_PeriodID", "value": $('#PeriodID').val() },

                        { "name": "CustomParam_EmpID", "value": $('#SelectedEmpID').val() }
                        );
                },
            });

        }

        $(document).ready(function () {
            // ADD ALL OPTIONS IN DROPDOWN

            $("#SelectedEmpID option").eq(1).before($("<option></option>").val("all").text("all"));



            // PERIODID CHANGE EVENT
            $(function () {
                $("#PeriodID").change(function () {

                    if ($('#SelectedEmpID').val()) {
                        ReInitializeDatatable();
                    } else {
                        $('#SelectedEmpID').prop('disabled', !$('#PeriodID').val());
                    }

                });
            });




            $(function () {
                $("#SelectedEmpID").change(function () {

                    ReInitializeDatatable();

                });
            });




        })



    </script>
}

我的问题是,当我选择'ALL'时,它会在viewmodel中加载大量数据,从而创建延迟,当我点击下一步时,例如。我在数据表中的第2页我的LINQ重新加载了我的DatatablesViewModel中的相同数据,我想过滤跳过并接收我的控制器的这一部分,以便它只需要所需的东西:

IEnumerable<DatatablesViewModel> viewmodel = (from a in db.tHREmployees
                  join b in db.tTADTRs on a.EmpID equals b.EmpID
                  join c in db.tHRJobGrades on a.JobGrdID equals c.JobGrdID
                  join d in db.tTAShifts on b.ShftID equals d.ShftID
                  where (b.LogDt >= PeriodDates.FromDt && b.LogDt <= PeriodDates.ToDt)
                  select new DatatablesViewModel { 
                      EmpID = a.EmpID, 
                      LastNm = a.LastNm, 
                      LogDT = b.LogDt, 
                      LogIn = b.LogIn, 
                      LogOut = b.LogOut, 
                      JobGradeDesc = c.JobGrdDesc, 
                      ShftNm = d.ShftNm 
                  });

我已经尝试添加.Skip(param.iDisplayStart).Take(param.iDisplayLength)如下所示:

 IEnumerable<DatatablesViewModel> viewmodel = (from a in db.tHREmployees
                  join b in db.tTADTRs on a.EmpID equals b.EmpID
                  join c in db.tHRJobGrades on a.JobGrdID equals c.JobGrdID
                  join d in db.tTAShifts on b.ShftID equals d.ShftID
                  where (b.LogDt >= PeriodDates.FromDt && b.LogDt <= PeriodDates.ToDt)
                  select new DatatablesViewModel { 
                      EmpID = a.EmpID, 
                      LastNm = a.LastNm, 
                      LogDT = b.LogDt, 
                      LogIn = b.LogIn, 
                      LogOut = b.LogOut, 
                      JobGradeDesc = c.JobGrdDesc, 
                      ShftNm = d.ShftNm
                  }).Skip(param.iDisplayStart).Take(param.iDisplayLength);

上面的代码导致数据表中的错误:http://puu.sh/gDLuD/1df68f53e8.png

这是我的页面截图:http://puu.sh/gDLFT/c022062092.png

0 个答案:

没有答案