LINQ query performance issue when fetching data from db in MVC Razor

Vishal I Patil

Problem Statement: I'm trying to bind Multi-table data from db to view using Linq query which is taking more time.I'm having around 10000 records in db.Someone suggested to use the IQueryable instead of IEnumerable,but does it affect my current code(In both View and Controller)?? Or without using that can I accomplish this??

What I should do in-order to increase the performance of loading the result?? What I'm doing wrong?? Please suggest me some better way of doing this...

Controller:

public ActionResult Index()
    {
        var result = (from pr in db.Prod.AsEnumerable()
                      join s in db.Shift.AsEnumerable() on pr.Shift equals s.ShiftID
                      join m in db.Module.AsEnumerable() on pr.Module equals m.ModuleID
                      select new GlobalModel()
                      {
                          prodModelIndex = pr,
                          prodModel = prodModel,
                          shiftModel = s,
                          moduleModel = m,
                          ddlShift = objTransactionGeneralController.GetAllShift(),
                          ddlModule = objTransactionGeneralController.GetAllModule()

                      }).ToList();
    return PartialView(result);

    }





   public TransGeneralModel GetAllModule()
    {

        objTransGeneralModel.ddlModule = (from m in db.Module.AsEnumerable()
                                           select new SelectListItem
                                           {
                                               Value = m.ModuleID.ToString(),
                                               Text = m.ModuleName,
                                           }).ToList();


        return objTransGeneralModel;
    }

    public TransGeneralModel GetAllShift()
    {

        objTransGeneralModel.ddlShift = (from s in db.Shift.AsEnumerable()
                                          select new SelectListItem
                                          {
                                              Value = s.ShiftID.ToString(),
                                              Text = s.ShiftName,
                                          }).ToList();


        return objTranGeneralModel;
    }

View:

@model IEnumerable<SIA.Models.Trans.GlobalModel>

@using GridMvc.Html

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Index";

    }

    <link rel="stylesheet" href="@Url.Content("~/Content/jquery.dataTables.min.css")">
    <script src="@Url.Content("~/Scripts/jquery-2.1.1.min.js")"></script>


    <h2>Details</h2>
    <hr />

    <div style="width: 1000px; padding-left: 70px">
        @Html.Partial("Create")
        <br />
    </div>

    <h5 class="pull-right">
        <b class="fa fa-keyboard-o" style="color: blue"></b>
        @Ajax.ActionLink("Edit", "ProdEdit", "Prod", new { }, new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "prod-details",
        HttpMethod = "GET",
    }, new { style = "color:blue" })
    </h5>

    <br />
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        if (Model.FirstOrDefault().prodModelIndex != null)
        { 

        <div id="prod-details">
            <table class="table table-striped" id="tblProdDetails">
                <thead>
                    <tr>

                        <th>
                            @Html.DisplayNameFor(model => model.FirstOrDefault().prodModelIndex.ProdID)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FirstOrDefault().prodModelIndex.Date)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FirstOrDefault().prodModelIndex.Module)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FirstOrDefault().productionModelIndex.Shift)
                        </th>

                        <th>
                            @Html.DisplayNameFor(model => model.FirstOrDefault().prodModelIndex.Hour)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.FirstOrDefault().prodModelIndex.Output)
                        </th>

                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr id="[email protected]">


                            <td>
                                @Html.DisplayFor(modelItem => item.prodModelIndex.ProdID)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.prodModelIndex.Date)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.moduleModel.ModuleName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.shiftModel.ShiftName)
                                @Html.HiddenFor(modelItem => item.prodModelIndex.Shift)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.prodModelIndex.Hour)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.prodModelIndex.Output)
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

        </div>
        }
    }

    <script>

        $(document).ready(function () {

            $('#tblProdDetails').dataTable({
                "order": [[1, "desc"], [3, "asc"]]

            });

        });
    </script>

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        @Scripts.Render("~/Scripts/jquery.dataTables.min.js")
        <script type='text/javascript'>
            $(function () {
                $('.datepicker').datepicker({
                    format: "dd M yyyy",
                }).on('changeDate', function (e) {
                    $(this).datepicker('hide');
                });
            })

        </script>
    }
Felipe Oriani

First, when you call a method like ToList(), AsEnumerable() or FirstOrDefault(), it will execute the query on the database. In your case, would be nice to remove them to hit a single query with joins.

var result = (from pr in db.Prod
                      join s in db.Shift on pr.Shift equals s.ShiftID
                      join m in db.Module on pr.Module equals m.ModuleID
                      select new GlobalModel()
                      {
                          prodModelIndex = pr,
                          prodModel = prodModel,
                          shiftModel = s,
                          moduleModel = m

                      }).ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Performance issue with linq query

From Dev

Issue in fetching data from db to the views in laravel 5.2

From Dev

Unable to save Linq query data to db in mvc 4

From Dev

Fetching data issue using simple like query

From Dev

Hibernate Search query fetching data from Actual database and not from Elastic DB indexes

From Dev

Issue with fetching data from excel (Using Java)

From Dev

Fetching data issue from mysql database in this code

From Dev

Have trouble in fetching the required data from DB

From Dev

Fetching Data from DB using PDO with Class

From Dev

Fetching Data from mysql db through PHP

From Dev

Performance Issue while fetching message from pending message list of a queue

From Dev

Performance comparison Sql query and Linq data query

From Dev

C# MVC 5.1 Razor 3.0 Performance Issue

From Dev

C# MVC 5.1 Razor 3.0 Performance Issue

From Dev

Issue with fetching query

From Dev

How to Group rows in List and give heading , when fetching Data from DB

From Dev

SQL query for fetching data from multiple tables

From Dev

Fetching data from MySQL, problems with diferent query

From Dev

LINQ query performance when applying Where clause

From Dev

Fetching data issue

From Dev

Issue fetching data in SQLite

From Dev

issue in fetching data in MySQL

From Dev

Issue when casting from DB

From Dev

Issue when casting from DB

From Dev

If-condition in linq query (fetching from multiple databases)

From Dev

Query Response for fetching receive payment records from quickbooks db?

From Dev

MVC5 strangely slow performance when using razor syntax

From Dev

prepare for segue when fetching data from firestore

From Dev

MVC Razor character issue

Related Related

HotTag

Archive