distinct values from a list using LINQ

distinct values from a list using LINQ to sql

This is basically used for get the distinct values from a linq list using GroupBy and Select with the help of C# and MVC 5. 

The code-sample as given below.

public IEnumerable<ServiceContract> getSContract(int id)
{
   ICollection<ServiceContract> serviceContract = mapper.MapDALServiceContracts2BAL(objDalServiceContract.getAllServiceContract());
   if (serviceContract.Count > 0)
   {
        if (id > 0)
        {
           serviceContract = serviceContract.Where(x => x.ContractID == id).ToList()
                    .Select(sc => new ServiceContract
                        {
                            ID = sc.ID,
                            EndDate = sc.EndDate,
                            StartDate = sc.StartDate,
                            InstallationDate = sc.InstallationDate,
                            TrialDays = sc.TrialDays
                        })
                     .GroupBy(x => x.EndDate)
                     .Select(y => y.First()).ToList();
          }
   };
   return serviceContract;
}

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

distinct values from a list using LINQ to sql distinct values from a list using LINQ to sql Reviewed by Anil Singh on 4:36 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^