【问题标题】:WCF POST Service With List<Object> as Parameters使用 List<Object> 作为参数的 WCF POST 服务
【发布时间】:2017-03-23 17:30:24
【问题描述】:

我是 WCF 服务的新手。我创建了一个 POST 方法,其中包含多个参数作为一个没有问题的对象,例如:

 public class BudgetTransactionRequest
{
    [DataMember]
    public string transaction_code { get; set; }
    [DataMember]
    public double? amount { get; set; }
    [DataMember]
    public DateTime request_date { get; set; }
    [DataMember]
    public string request_status { get; set; }
    [DataMember]
    public string owner { get; set; }
    [DataMember]
    public string opportunity { get; set; }
    [DataMember]
    public string project { get; set; }
    [DataMember]
    public string application_type { get; set; }
    [DataMember]
    public string category { get; set; }
    [DataMember]
    public string claim_status { get; set; }
}

我的 POST 方法可以运行良好。但是现在,我需要像这样传递多个对象参数:

[DataContract(Namespace = "http://mlpt-web.com/CRM/services")]
    public class CreateBudget
    {
        [DataMember]
        public string pr_code { get; set; }

        [DataMember]
        public List<BudgetTransactionRequest> transactions { get; set; } 

    }

在服务器端,我如何读取列表中对象的每个参数?我需要获取对象内的每个参数,以便我可以运行其他进程。

我试过了:

 public string CreateBudget(CreateBudget cbreq)
        {if (!String.IsNullOrEmpty(cbreq.pr_code))
            {
                List<BudgetTransactionRequest> bt = new List<BudgetTransactionRequest>();
                for (int i = 0; i < bt.Count - 1; i++)
                {
                        if (!string.IsNullOrEmpty(bt[i].transaction_code))
                        {
                            budgettrx["new_name"] = bt[i].transaction_code;
                        }
                        _service.Create(budgettrx);
                    }
                }

            }
            return "Done";
        }

但我不确定这是否可行。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    您可以使用 for each loop go 遍历列表并获取其中的对象。

    foreach (var myObj in yourList) {
    
    }
    

    【讨论】:

    • 喜欢这个? foreach (var bt in cbreq.transactions) { string tr = bt.transaction_code; }
    • 是的,你试过了吗
    • 好的,谢谢您的建议。我需要编写客户端代码,所以我知道代码是有效的。
    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2016-08-13
    • 2012-01-12
    相关资源
    最近更新 更多