-1

我想在新语句中使用 foreach 循环分配属性 subdecision_typex_value ,它是 DtoReport 类的属性。

这可能吗?是否有意义 ?

public DtoReport Get(Report repResp)
   return new DtoReport()
   {
      archivingId = repResp.archivingId.ToString(),
      dateCreated = DateTime.Now,

      //I'D LIKE TO DO IT THAT WAY IS IT POSSIBLE SOMEHOW ?
      foreach(Subdecision d in repResp.decisionMatrix.subdecisions){
         if(d.type == "SOME VALUE"){
            //Dynamically assign DtoReport subdecision_typex_value Property 
            subdecision_typex_value = d.value                   
         }
      }
      //END

      anotherProperty = repResp.AnotherProperty
   }
4

1 回答 1

1

您可以使用 Linq:

return new DtoReport()
{
  archivingId = repResp.archivingId.ToString(),
  dateCreated = DateTime.Now,
  subdecision_typex_value = repResp.decisionMatrix.subdecisions
                           .Where(d => d.type == "SOME VALUE")
                           .Select(d => d.value)
                           .FirstOrDefault(),
  anotherProperty = repResp.AnotherProperty
}

请注意,您的方法很可能不是它应该做的。您正在枚举所有子决策,然后您将使用type=="SOME VALUE". 我假设您想采用这种类型的第一个值,对吗?

于 2012-10-30T08:30:55.723 回答