LINQ: Read Distinct Record from Data Table

table
If I write query to fetch City column values:

  1. public JsonResult getEmpCity()  
  2.         {  
  3.             using (CompanyDBEntities dc = new CompanyDBEntities())  
  4.             {  
  5.                 var query = (from p in dc.Emp_Information  
  6.                              select p.City).ToList();  
  7.                 return new JsonResult  
  8.                 {  
  9.                     Data = query.ToList(),  
  10.                     JsonRequestBehavior = JsonRequestBehavior.AllowGet  
  11.                 };  
  12.             }  
  13.         } 
Debug this call and see result.

result

Now write your code like below:
  1. public JsonResult getEmpCity()  
  2.         {  
  3.             using (CompanyDBEntities dc = new CompanyDBEntities())  
  4.             {  
  5.                 var query = (from p in dc.Emp_Information  
  6.                              select p.City).Distinct().ToList();  
  7.                 return new JsonResult  
  8.                 {  
  9.                     Data = query.ToList(),  
  10.                     JsonRequestBehavior = JsonRequestBehavior.AllowGet  
  11.                 };  
  12.             }  
  13.         }  
And See result.

result

 

Ebook Download
View all
Learn
View all