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;


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://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions