Gcobani Mkontwana

Gcobani Mkontwana

  • 1k
  • 1.3k
  • 109.5k

How to limit records not to duplicate data in linq c#?

Apr 29 2021 5:27 PM
Hi Team
 
I have a method that check when user insert 'Year' and 'Week', but my fitering on excel is wrong. Meaning if I search by year 2020 and week 43. First check on 42 Week and Year 2020 downwards. How do i fix this not to happen?. What i want when i search by Year, the record on excel must do the same. Please assist me, thanks.
 
  1. // Model  
  2. public class ExtractionViewModel  
  3.     {  
  4.         public string Year { getset; }  
  5.   
  6.         public int Week { getset; }  
  7.   
  8.           
  9.         [DataType(DataType.Date)]  
  10.         public DateTime Day { getset; }  
  11.   
  12.         //public string VW240 { get; set; }  
  13.   
  14.         public string VW250 { getset; }  
  15.   
  16.         public string VW270 { getset; }  
  17.   
  18.        public string VW2502PA { getset; }  
  19.   
  20.         public string VW270PA { getset; }  
  21.         //public List<ExtractionViewModel> sessionList { get; set; }  
  22.           
  23.           
  24.      
  25.     }  
  26.   
  27. // Export to excel  
  28.   
  29.      public void ExportToExcel()  
  30.         {  
  31.   
  32.             var v = new GridView();  
  33.             v.DataSource = this.GetExtractionViewModels();  
  34.             v.DataBind();  
  35.             Response.ClearContent();  
  36.             Response.Buffer = true;  
  37.             Response.ClearHeaders();  
  38.             Response.AddHeader("content-disposition""attachment; filename=ExtractionRecords.xls");  
  39.             Response.ContentType = "application/vnd.ms-excel";  
  40.             Response.Charset = "";  
  41.             StringWriter objStringWriter = new StringWriter();  
  42.             HtmlTextWriter htmlTextWriter = new HtmlTextWriter(objStringWriter);  
  43.             v.RenderControl(htmlTextWriter);  
  44.             Response.Output.Write(objStringWriter.ToString());  
  45.             Response.Flush();  
  46.             Response.End();  
  47.   
  48.   
  49.         }  
  1. // controller  
  2. public ActionResult Extract()  
  3.         {  
  4.             int week;  
  5.             try  
  6.             {  
  7.                 week = int.Parse(TempData["week"].ToString());  
  8.             }  
  9.             catch (Exception e)  
  10.             {  
  11.                 week = 0;  
  12.             }  
  13.   
  14.   
  15.             string year;  
  16.             try  
  17.             {  
  18.                 year = (TempData["year"].ToString());  
  19.             }  
  20.             catch (Exception e)  
  21.             {  
  22.                 year = "";  
  23.             }  
  24.   
  25.             if (year == "" && week == 0)  
  26.             {  
  27.   
  28.                 return RedirectToAction("GetExtractionViewModelsNoParam");  
  29.   
  30.   
  31.             }  
  32.             else if (year == "" && week > 0)  
  33.             {  
  34.   
  35.                 return RedirectToAction("GetExtractionViewModelsWeekParam");  
  36.   
  37.             }  
  38.             else  
  39.             {  
  40.   
  41.                 return RedirectToAction("GetExtractionViewModelsYearAndWeekParam");  
  42.             }  
  43.         }  
  44.   
  45.   
  46.   
  47.     }  
  48.   
  49.  public ActionResult GetExtractionViewModelsYearAndWeekParam()  
  50.         {  
  51.             int week = int.Parse(TempData["week"].ToString());  
  52.   
  53.             string year = (TempData["year"].ToString());  
  54.             ProductionManagementEntities db = new ProductionManagementEntities();  
  55.   
  56.   
  57.             var schedueList = (from m in db.Weeks  
  58.                                
  59.                                select new  
  60.                                {  
  61.   
  62.                                });  
  63.              
  64.             var query = (from p in db.ProductionDays  
  65.                          join m in db.Models on p.ProductionDate equals m.CreatedDate  
  66.                          join w in db.Weeks on p.WeekId equals w.WeekId  
  67.                          orderby w.Year descending, p.CreatedDate descending, w.WeekNum descending  
  68.                          select new { w.Year, p.CreatedDate, w.WeekNum });  
  69.   
  70.             List extractionViewModels = new List();  
  71.             ExtractionViewModel extractionViewModel;  
  72.             foreach (var item in query)  
  73.             {  
  74.                 extractionViewModel = new ExtractionViewModel();  
  75.                 extractionViewModel.Year = item.Year;  
  76.                 extractionViewModel.Week = item.WeekNum;  
  77.                 extractionViewModel.Day = item.CreatedDate;  
  78.   
  79.                 extractionViewModels.Add(extractionViewModel);  
  80.   
  81.             }  
  82.   
  83.             //TempData["scheduleList"] = query;  
  84.             return RedirectToAction("ExportToExcel");  
  85.   
  86.         }  
  87.  public ActionResult GetExtractionViewModelsWeekParam()  
  88.         {  
  89.             int week = int.Parse(TempData["week"].ToString());  
  90.             ProductionManagementEntities db = new ProductionManagementEntities();  
  91.             var schedueList = (from m in db.Weeks  
  92.                                  
  93.                                select new  
  94.                                {  
  95.   
  96.                                });  
  97.              
  98.             var query = (from p in db.ProductionDays  
  99.                          join m in db.Models on p.ProductionDate equals m.CreatedDate  
  100.                          join w in db.Weeks on p.WeekId equals w.WeekId  
  101.                          orderby w.Year descending, p.CreatedDate descending, w.WeekNum descending  
  102.                          select new { w.Year, p.CreatedDate, w.WeekNum });  
  103.   
  104.             List extractionViewModels = new List();  
  105.             ExtractionViewModel extractionViewModel;  
  106.             foreach (var item in query)  
  107.             {  
  108.                 extractionViewModel = new ExtractionViewModel();  
  109.                 extractionViewModel.Year = item.Year;  
  110.                 extractionViewModel.Week = item.WeekNum;  
  111.                 extractionViewModel.Day = item.CreatedDate;  
  112.   
  113.                 extractionViewModels.Add(extractionViewModel);  
  114.   
  115.             }  
  116.  public ActionResult GetExtractionViewModelsNoParam()  
  117.         {  
  118.   
  119.             ProductionManagementEntities db = new ProductionManagementEntities();  
  120.   
  121.   
  122.             var schedueList = (from m in db.Weeks  
  123.                                  
  124.                                select new  
  125.                                {  
  126.   
  127.                                });  
  128.               
  129.             var query = (from p in db.ProductionDays  
  130.                          join m in db.Models on p.ProductionDate equals m.CreatedDate  
  131.                          join w in db.Weeks on p.WeekId equals w.WeekId  
  132.                          orderby w.Year descending, p.CreatedDate descending, w.WeekNum descending  
  133.                          select new { w.Year, p.CreatedDate, w.WeekNum });  
  134.   
  135.             List extractionViewModels = new List();  
  136.             ExtractionViewModel extractionViewModel;  
  137.             foreach (var item in query)  
  138.             {  
  139.                 extractionViewModel = new ExtractionViewModel();  
  140.                 extractionViewModel.Year = item.Year;  
  141.                 extractionViewModel.Week = item.WeekNum;  
  142.                 extractionViewModel.Day = item.CreatedDate;  
  143.   
  144.                 extractionViewModels.Add(extractionViewModel);  
  145.   
  146.             }