Guest User

Guest User

  • Tech Writer
  • 611
  • 128k

Remove duplicate record in list

Aug 10 2020 6:47 AM
I have get record by id. It's API run successfully. But i have face issue same record get multiple time
  1. public SupplementResponse GetSupplementsList(SupplemetParam obj_parameter)  
  2. {  
  3. SupplementResponse obj_result = new SupplementResponse();  
  4. List<SupplementListDetail> lst = new List<SupplementListDetail>();  
  5. var PDFURL = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("AppSettings")["SupplementUrl"];  
  6. try  
  7. {  
  8. var matchingdata = (from c in _context.TblSupplementDetail  
  9. join sc in _context.SupplementMaster on c.SupplementId equals sc.SupplementId  
  10. where c.UserId == obj_parameter.UserId  
  11. select new  
  12. {  
  13. sc.SupplementId,  
  14. sc.SupplementType,  
  15. sc.SupplementPdf,  
  16. c.SupplementStatus  
  17. }).ToList();  
  18. if (matchingdata.Count > 0)  
  19. {  
  20. foreach (var item in matchingdata)  
  21. {  
  22. SupplementListDetail obj_supplement = new SupplementListDetail();  
  23. obj_supplement.SupplementsId = item.SupplementId;  
  24. obj_supplement.SupplementType = item.SupplementType;  
  25. obj_supplement.SupplementPDF = PDFURL + "" + item.SupplementPdf;  
  26. obj_supplement.SupplementStatus = Convert.ToInt32(item.SupplementStatus);  
  27. lst.Add(obj_supplement);  
  28. }  
  29. }  
  30. var Supplement_List = _context.SupplementMaster.ToList();  
  31. {  
  32. if(Supplement_List .Count >0)  
  33. {  
  34. foreach (var item in Supplement_List)  
  35. {  
  36. if(item.SupplementStatus==false)  
  37. {  
  38. SupplementListDetail obj_supplement = new SupplementListDetail();  
  39. obj_supplement.SupplementsId = item.SupplementId;  
  40. obj_supplement.SupplementType = item.SupplementType;  
  41. obj_supplement.SupplementPDF = PDFURL + "" + item.SupplementPdf;  
  42. obj_supplement.SupplementStatus = Convert.ToInt32(item.SupplementStatus);  
  43. lst.Add(obj_supplement);  
  44. }  
  45. }  
  46. }  
  47. }  
  48. obj_result.Response = 1;  
  49. obj_result.ErrorMessage = "No Error Found";  
  50. List<SupplementListDetail> uniqueLst = lst.Distinct().ToList(); //i have use this here.  
  51. obj_result.lst_Supplement = uniqueLst;  
  52. It's me classes.  
  53. public class SupplemetParam  
  54. {  
  55. public int UserId { getset; }  
  56. }  
  57. public class SupplementResponse  
  58. {  
  59. public int Response { getset; }  
  60. public string ErrorMessage { getset; }  
  61. public IEnumerable<SupplementListDetail> lst_Supplement { getset; }  
  62. }  
  63. public class SupplementListDetail  
  64. {  
  65. public int SupplementsId { getset; }  
  66. public string SupplementType { getset; }  
  67. public string SupplementPDF { getset; }  
  68. public int SupplementStatus { getset; }  
  69. }  

Answers (10)