Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How to get perfect Result within same table and result without duplicate record when searching via LINQ MVC

$
0
0


Dear all expert,

I am a new  LINQ trainer and  trying to join 1 table in a query with Linq to get data from all combine 3 tables but unlucky getting a unique result on MVC web  . I getting mistake in my coding  for Controller , View and model part in MVC. Please help me on this issue and would be greatly appreciated. Thank You .

Below is My SQL query result:

<div> SELECT a.[Admin_Id] ,a.[Admin_Name], a.[Admin_Status],a.[Admin_Remarks]</div> <div>             ,a.[Admin_CreateBy] ,a2.[Admin_Name],a.[Admin_UpdateBy],a1.Admin_Name</div> <div>           FROM [ABC].[dbo].[Admin]a </div> <div>            join [ABC].[dbo].[Admin]a1 on a.[Admin_UpdateBy] = a1.Admin_Id</div> <div>            join [ABC].[dbo].[Admin]a2 on  a.[Admin_CreateBy] = a2.Admin_Id</div> <div> where a.[Admin_Name] like '%l%' or a.[Admin_Remarks] like '%l%'</div> <div> or a1.[Admin_Name] like '%l%' or a2.[Admin_Name] like '%l%'</div> <div></div> <div>SQL Result : </div> <div>

Admin_IdAdmin_NameAdmin_StatusAdmin_RemarksAdmin_CreateByAdmin_NameAdmin_CreateDateAdmin_UpdateByAdmin_NameAdmin_UpdateDate
A0005Admin L1Superadmin3A0001ABCDE57:22.5A0001ABCDE40:47.9
A0010Admin C2NULLA0005Admin L53:32.4A0001ABCDE54:51.3
A0011Admin D2NULLA0005Admin L55:59.9A0001ABCDE10:45.3

</div>

Now, i am getting unique result from Web that Create By and Update By Column is null value and sometime will get duplicate record when searching by name . example : "a"

NameStatusRemarksCreate ByAdmin_CreateDateUpdate ByAdmin_UpdateDate
Admin LActiveSuperadmin3 14/10/2020 14/10/2020
Admin CInActive Admin L20/10/2020 27/10/2020
Admin DInActive Admin L20/10/2020 27/10/2020

Here Coding in Model Part:

using ABC.Edmx;using PagedList;using System.Collections.Generic;namespace ABC{    public class EntityWM    {           //Get A row DB        public Status status { get; set; }        public Admin AdminModel { get; set; }  // a row from db               // get whole list DB        public List<Status> statusList { get; set; }        public AdminModelList { get; set; }                 // pager list        public IPagedList<Admin> AdminPageList { get; set; }          }}

Coding in Controller :

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using ABC.Edmx;
using ABC.Models;
using PagedList;
namespace ABC.Controllers
{
    public class AdminController : Controller
    {
        private ABCEntities _edmx = new ABCEntities();

        // GET: Admin
        [HttpGet]
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)

        {
           
            EntityWM _Rmodel = null;
            _Rmodel = new EntityWM();
            
           
            _Rmodel.AdminModelList = _edmx.Admins.ToList().Select(p => new Admin
            {
                Admin_Id = p.Admin_Id,
                Admin_Name = p.Admin_Name,
                Admin_Status = p.Admin_Status,
                Admin_Remarks = p.Admin_Remarks,
                Admin_CreateBy = p.Admin_CreateBy,
                Admin_CreateDate = p.Admin_CreateDate,
                Admin_UpdateBy = p.Admin_UpdateBy,
                Admin_UpdateDate = p.Admin_UpdateDate

            }).ToList();

            _Rmodel.statusList = _edmx.Status.ToList().Select(p => new Status
            {
                Status_Id = p.Status_Id,
                Status_Desc = p.Status_Desc
            }).ToList();
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            /*
            SELECT a.[Admin_Id] ,a.[Admin_Name] ,a.[Admin_Password],a.[Admin_Status],a.[Admin_Remarks]
             ,a.[Admin_CreateBy] ,a.[Admin_CreateDate],a2.[Admin_Name],a.[Admin_UpdateBy],a1.Admin_Name
             ,a.[Admin_UpdateDate]
            FROM [OnlineTest_DEV2].[dbo].[Admin]a
            join [OnlineTest_DEV2].[dbo].[Admin]a1 on a.[Admin_UpdateBy] = a1.Admin_Id 
            join [OnlineTest_DEV2].[dbo].[Admin]a2 on  a.[Admin_CreateBy] = a2.Admin_Id
            */

            if (!String.IsNullOrEmpty(searchString))

            {
                _Rmodel.AdminModelList = (from s in _Rmodel.AdminModelList
                                          join a in _Rmodel.AdminModelList on s.Admin_CreateBy equals a.Admin_Id into AdminTable1
                                          from a in AdminTable1.ToList()
                                          join a1 in _Rmodel.AdminModelList on s.Admin_UpdateBy equals a1.Admin_Id into AdminTable2
                                          from a1 in AdminTable2.ToList()
                                          where s.Admin_Name?.ToLower().Contains(searchString.ToLower()) == true
               || s.Admin_Remarks?.ToLower().Contains(searchString.ToLower()) == true ||
               a.Admin_Name?.ToLower().Contains(searchString.ToLower()) == true
               || a1.Admin_Name?.ToLower().Contains(searchString.ToLower()) == true
                                          select new Admin
                                          {
                                              Admin_Id = s.Admin_Id,
                                              Admin_Name = s.Admin_Name,
                                              Admin_Status = s.Admin_Status,
                                              Admin_Remarks = s.Admin_Remarks,
                                              Admin_CreateDate = s.Admin_CreateDate,
                                              Admin_UpdateDate = s.Admin_UpdateDate,
                                              Admin_CreateBy = s.Admin_CreateBy,
                                              Admin_UpdateBy = s.Admin_UpdateBy
                                              //Admin_UpdateBy = a1.Admin_Id,
                                              //Admin_CreateBy = a.Admin_Id

                                          }).ToList();
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);

            _Rmodel.AdminPageList = _Rmodel.AdminModelList.ToPagedList(pageNumber, pageSize);

            return View(_Rmodel);
        }



-------------------------------------------------------------------------------------------------------

Coding in csthml:

@model ABC.Models.EntityWM

@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /><h2>Admin Index</h2>

@using (Html.BeginForm("Index", "Admin", FormMethod.Get))
{<p>
        @Html.TextBox("searchString", ViewBag.CurrentFilter as string, new { @placeholder = "Search..." })<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-search"></span></button><a class="btn btn-success pull-right" data-modal="" onclick="@("window.location.href='" + @Url.Action("Create", "Admin") + "'")" id="btnCreate"><span class="glyphicon glyphicon-plus"></span></a></p>

}

<table class="table"><tr><th>
            @Html.ActionLink(Model.ResourcesModel.FirstOrDefault(x => x.Resources_Name == "Name").Resources_Value, "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })</th><th>
            @Html.ActionLink(Model.ResourcesModel.FirstOrDefault(x => x.Resources_Name == "Status").Resources_Value, "Index", new { sortOrder = ViewBag.StatusSortParm, currentFilter = ViewBag.CurrentFilter })</th><th>
            @Html.ActionLink("Remarks", "Index", new { sortOrder = ViewBag.RemarksSortParm, currentFilter = ViewBag.CurrentFilter })</th><th>
            @Html.ActionLink("Create By", "Index", new { sortOrder = ViewBag.CreateBySortParm, currentFilter = ViewBag.CurrentFilter })</th><th>
            @Html.ActionLink("Create Date", "Index", new { sortOrder = ViewBag.CreateDateSortParm, currentFilter = ViewBag.CurrentFilter })</th><th>
            @Html.ActionLink("Update By", "Index", new { sortOrder = ViewBag.UpdateBySortParm, currentFilter = ViewBag.CurrentFilter })</th><th>
            @Html.ActionLink("Update Date", "Index", new { sortOrder = ViewBag.UpdateDateSortParm, currentFilter = ViewBag.CurrentFilter })</th><th></th></tr>

    @foreach (var item in Model.AdminPageList)

    {
        <tr><td>
                @Html.DisplayFor(modelItem => item.Admin_Name)</td><td>
                @Html.DisplayFor(modelItem => Model.statusList.FirstOrDefault(x => x.Status_Id == item.Admin_Status).Status_Desc)</td><td>
                    @Html.DisplayFor(modelItem => item.Admin_Remarks)</td><td>
                    @Html.DisplayFor(modelItem => Model.AdminModelList.Where(x => x.Admin_Id == item.Admin_CreateBy).FirstOrDefault().Admin_Name)</td><td>
                    @Html.DisplayFor(modelItem => item.Admin_CreateDate)</td><td>
                    @Html.DisplayFor(modelItem => Model.AdminModelList.Where(x => x.Admin_Id == item.Admin_UpdateBy).FirstOrDefault().Admin_Name)</td><td>
                    @Html.DisplayFor(modelItem => item.Admin_UpdateDate)</td><td>
                    @Html.ActionLink(" ", "Edit", new { id = item.Admin_Id }, new { @class = "glyphicon glyphicon-edit" })
                    @Html.ActionLink(" ", "Details", new { id = item.Admin_Id }, new { @class = "glyphicon glyphicon-th-list" })
                    @Html.ActionLink(" ", "Delete", new { id = item.Admin_Id }, new { @class = "glyphicon glyphicon-trash" })</td></tr>
    }</table><br />
Page @( Model.AdminPageList.PageCount < Model.AdminPageList.PageNumber ? 0 : Model.AdminPageList.PageNumber) of @Model.AdminPageList.PageCount

@Html.PagedListPager(Model.AdminPageList, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>