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)
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;
}