我有以下课程:
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; }
}
基本上,如果条件为真,我想创建一个新对象,否则我想更新ProviderQualificationDetail.ProviderQualificationTime
,其中ProviderQualificationDetail.ProviderName == providerName
是否可以使用lambda表达式?
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
}
}
答案 0 :(得分:2)
在其他部分中这样:
foreach (var item in providerQualificationDetail.Where(x => x.ProviderName== providerName))
{
item.ProviderQualificationTime = Math.Round(processingTime).ToString();
}
或者
providerQualificationDetail.Where(x => x.ProviderName == ProviderName).Select(c =>
{
c.ProviderQualificationTime = "new time ";
return providerQualificationDetail;
}).ToList();
或者
providerQualificationDetail.ForEach(x =>
{
if(x.ProviderName == ProviderName)
x.ProviderQualificationTime = "new time";
});
答案 1 :(得分:0)
试试这个......
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
{
var qualificationDetail = providerQualificationDetail.SingleOrDefault(p => p.ProviderName.Equals(providerName));
//Assing your values here
//example;
qualificationDetail.ProviderName = NewProviderName.ToString();
providerQualificationDetail.SaveChanges();
}
}