【问题标题】:How to use Linq add list data to list sub class?如何使用 Linq 添加列表数据到列表子类?
【发布时间】:2019-03-01 09:37:54
【问题描述】:

我想将列表数据(来自类)添加到子类列表中,以便将 JSON FORMAT 发送到其他 api。

public class InPaymentDetailResponse
{
       public List<InPaymentDetail> Data { get; set; }        
}

public class InPaymentDetail
{
       public string runningno { get; set; }
       public decimal totalpremium { get; set; }
}

public class InformationRequest
{        
    ..... 
    ..... 
    public List<CreateDetailRequest> _detailData { get; set; }
    public ExPaymentRequest _payment { get; set; }
}

public class ExPaymentRequest
{
   .....
   .....    
   public ExCashtransaction _cash { get; set; }

}

public class ExCashtransaction
{        
     public string createby { get; set; }
     public DateTime createdate { get; set; }
     public List<ExCashtransactionDetailsRequest> details { get; set; }
}

public class ExCashtransactionDetailsRequest
{
        public string runningno { get; set; }
        public decimal amount { get; set; }
}

C#代码

private async Task<.....> CreateInfo(InformationRequest r)
{

InPaymentDetailResponse resPaymentDetail = new InPaymentDetailResponse();
resPaymentDetail.Data = new List<InPaymentDetail>();

foreach (var item in r._detailData)
{
    .....
    ..... //Process Data
    .....

    var resPaymentDetailData = new InPaymentDetail
    {
        //Add List Data To New Object
        runningno = item.runningno,
        totalpremium = item.totalpremium
    };
    resPaymentDetail.Data.Add(resPaymentDetailData);
}

HttpResponseMessage response = new HttpResponseMessage();
foreach (var res in resPaymentDetail.Data.ToList())
{
     //i want add
     //req._payment._cash.details.runningno = res.runningno  //Ex. 10
     //req._payment._cash.details.amount = res.amount  //Ex. 99.9

     //next loop
     //req._payment._cash.details.runningno = res.runningno  //Ex. 20
     //req._payment._cash.details.amount = res.amount  //Ex. 23.2
}

//sent to other api
response = await client.PostAsJsonAsync("", req._payment._cash);

.....
.....

}

我想要将列表数据添加到 req._payment._cash 时的结果代码: (因为要在下一个过程中使用它)

"createby": "system",
  "createdate": 26/09/2018",
  "details": [
    {
      "runningno": "10", //before add data to list is null
      "amount": 99.9 //before add data to list is null
    },
     {
      "runningno": "20", //before add data to list is null
      "amount": 23.2 //before add data to list is null
     }
   ]

请帮帮我。提前致谢。

【问题讨论】:

    标签: c# list linq class subclass


    【解决方案1】:
    List<ExCashtransactionDetailsRequest> detailsObj = new List<ExCashtransactionDetailsRequest>();
    
    foreach (var res in resPaymentDetail.Data.ToList())
    {
    
    detailsObj.Add(new  ExCashtransactionDetailsRequest(){ runningno  =res.runningno, amount = res.amount });
    }
    

    最后,将详细信息对象添加到您的属性中

    req._payment._cash.details = detailsObj;
    

    【讨论】:

    • 非常感谢。是工作。 :D
    • 很高兴知道(y)
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 2010-09-16
    • 2011-02-03
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多