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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.