Huma Ali Huma Ali - 29 days ago 8
C# Question

Assign Value in List using lambda expression

I have following classes:

public class ProviderQualificationTimeViewModel
{
public string SessionId { get; set; }
public List<ProviderQualificationDetail> ProviderQualificationDetails { get; set; }
}

public class ProviderQualificationDetail
{
public string ProviderName { get; set; }
public string ProviderQualificationTime { get; set; }
public string TotalServiceableOffers { get; set; }
}


Basically, I want to create a new object if condition is true else I want to update
ProviderQualificationDetail.ProviderQualificationTime
where ProviderQualificationDetail.ProviderName == providerName
Is it possible using lambda expression?

List<ProviderQualificationDetail> providerQualificationDetail = new List<ProviderQualificationDetail>();

foreach (ProviderModel providers in allProviders)
{
if(!providerQualificationDetail.Any(p=>p.ProviderName.Contains(providerName)))
{
ProviderQualificationDetail ProviderQualificationDetail = new ProviderQualificationDetail();
ProviderQualificationDetail.ProviderName = providerName;
ProviderQualificationDetail.ProviderQualificationTime = Math.Round(processingTime).ToString();
ProviderQualificationDetail.TotalServiceableOffers = "Not serviceable";
providerQualificationDetail.Add(ProviderQualificationDetail);
}
else
{
//Lambda expression here
}
}

Answer

like this in else part:

foreach (var item in providerQualificationDetail.Where(x => x.ProviderName== providerName))
{
item.ProviderQualificationTime = Math.Round(processingTime).ToString();
}

Or

providerQualificationDetail.Where(x => x.ProviderName == ProviderName).Select(c =>                      
{
c.ProviderQualificationTime = "new time ";
return providerQualificationDetail;
}).ToList();

Or

providerQualificationDetail.ForEach(x =>
{
if(x.ProviderName == ProviderName)
x.ProviderQualificationTime = "new time";
});