Guest User

Guest User

  • Tech Writer
  • 611
  • 116.7k

Remove duplicacy in linq c# .

Aug 10 2020 12:16 PM
I have an issue. How to remove duplicate record from list. It's me code please check.
  1. public class SupplementResponse  
  2. {  
  3. public int Response { getset; }  
  4. public string ErrorMessage { getset; }  
  5. public List<SupplementListDetail> lst_Supplement { getset; }  
  6. public List<SupplementDetail> lstOf_Supplement { getset; }  
  7. }  
  8. public class SupplementDetail  
  9. {  
  10. public int SupplementsId { getset; }  
  11. public string SupplementType { getset; }  
  12. public string SupplementPDF { getset; }  
  13. public int SupplementStatus { getset; }  
  14. }  
  15. public class SupplementListDetail  
  16. {  
  17. public int SupplementsId { getset; }  
  18. public string SupplementType { getset; }  
  19. public string SupplementPDF { getset; }  
  20. public int SupplementStatus { getset; }  
  21. }  
  22. SupplementResponse obj_result = new SupplementResponse();  
  23. List<SupplementListDetail> lst = new List<SupplementListDetail>();  
  24. List<SupplementDetail> list = new List<SupplementDetail>();  
  25. var result = lst.Union(list).ToList(); //Face Issue here   
  26. var PDFURL = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("AppSettings")["SupplementUrl"];  
  27. try  
  28. {  
  29. var matchingdata = (from c in _context.TblSupplementDetail  
  30. join sc in _context.SupplementMaster on c.SupplementId equals sc.SupplementId  
  31. where c.UserId == obj_parameter.UserId  
  32. select new  
  33. {  
  34. sc.SupplementId,  
  35. sc.SupplementType,  
  36. sc.SupplementPdf,  
  37. c.SupplementStatus  
  38. }).ToList();  
  39. if (matchingdata.Count > 0)  
  40. {  
  41. foreach (var item in matchingdata)  
  42. {  
  43. SupplementListDetail obj_supplement = new SupplementListDetail();  
  44. obj_supplement.SupplementsId = item.SupplementId;  
  45. obj_supplement.SupplementType = item.SupplementType;  
  46. obj_supplement.SupplementPDF = PDFURL + "" + item.SupplementPdf;  
  47. obj_supplement.SupplementStatus = Convert.ToInt32(item.SupplementStatus);  
  48. lst.Add(obj_supplement);  
  49. }  
  50. }  
  51. var Supplement_List = _context.SupplementMaster.ToList();  
  52. {  
  53. if(Supplement_List .Count >0)  
  54. {  
  55. foreach (var item in Supplement_List)  
  56. {  
  57. SupplementDetail obj_supplement = new SupplementDetail();  
  58. obj_supplement.SupplementsId = item.SupplementId;  
  59. obj_supplement.SupplementType = item.SupplementType;  
  60. obj_supplement.SupplementPDF = PDFURL + "" + item.SupplementPdf;  
  61. obj_supplement.SupplementStatus = Convert.ToInt32(item.SupplementStatus);  
  62. list.Add(obj_supplement);  
  63. }  
  64. }  
  65. }  
  66. obj_result.Response = 1;  
  67. obj_result.ErrorMessage = "No Error Found";  
  68. obj_result.lst_Supplement = lst;  
  69. obj_result.lstOf_Supplement = list;  
  70. }  
  71. catch (Exception ex)  
  72. {  
  73. obj_result.Response = 0;  
  74. obj_result.ErrorMessage = "Internal Server Error";  
  75. }  
  76. return obj_result;   
it's my code. It's more important for me

Answers (1)