Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

LINQ Read Distinct Record from Data Table

Article:
Viewed:  728 
Posted On:  5/4/2016 1:58:28 AM 
How we can read distinct record from Data Table using LINQ 

 

By Using LINQ how we can read distinct values. Below is my Data Table from which I will read distinct column value.

1.png

Image 1.

If I write query to fetch City column values:

public JsonResult getEmpCity()
{
        using (CompanyDBEntities dc = new CompanyDBEntities())
        {
            var query = (from p in dc.Emp_Information
                         select p.City).ToList();
            return new JsonResult
                {
                    Data = query.ToList(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
        }
 

Debug this call and see result.

2.png

Image 2.

Now write your code like below:

public JsonResult getEmpCity()
        {
            using (CompanyDBEntities dc = new CompanyDBEntities())
            {
                var query = (from p in dc.Emp_Information
                             select p.City).Distinct().ToList();
                return new JsonResult
                {
                    Data = query.ToList(),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
        }
 

And See result.

 

3.png

 

Image 3.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us