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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book