当我们在数据协定的对象中有不同的数据时,如何执行语句?

时间:2012-07-23 13:24:56

标签: c# wcf

我创建了一个包含请求和响应数据契约的API。此API将响应作为注释行。我有一个请求数据,其中包含数据成员

  • cardNumber
  • 放在methodID
  • noteline1,noteline2,...,noteline18

我有一个响应数据合同,其中包含18个数据成员,如noteline1,noteline2,...,noteline18。代码如下:

public CBNotesInqAndMaintResponse CBNote(CBNotesInqAndMaintRequest request)
{
    CBNotesInqAndMaintResponse response;
    try
    {
        var processCardBaseEnquiry = new CBNotesInqAndMaint();
        request.methodID = "01";
        response = processCardBaseEnquiry.Execute(request, clientIpAddress);
        processCardBaseEnquiry = null;
        return response;
    }
    catch(Exception ex){}

    if (response != null)
    {
        if (response.noteLine1 != null || response.noteLine1 != "")
        {
            request.methodID = "02";
            request.noteLine1 = response.noteLine1;
            processCardBaseEnquiry.Execute(request, clientIpAddress);
        }
        else if (response.noteLine2 != null || response.noteLine2 != "")
        {
            request.methodID = "02";
            response.noteLine2 = response.noteLine2;
            processCardBaseEnquiry.Execute(request, clientIpAddress);
        }

    // now I want to assign values to the request data from the response 
    // that I have received with methodID="02" and again execute the 
    // service to return the desired response. 

    ...
}

我想在for循环的帮助下完成它,因为它应该减少代码行。如何通过分配noteline文本的值来使用for循环

1 个答案:

答案 0 :(得分:2)

类似的东西?

//----------------------
//Response class
public class Responce
{
    List<string> MyData {get;set;}

    public Response()
    {
        MyData = new List<string>();
    }
}

//----------------------
//create response
var response = Response();
MyData.Add("result 1");
MyData.Add("result 2");

//----------------------
//and then later when you process it
var data = responce.MyData
foreach(string line in data)
{
    if(String.IsNullOrEmpty(line)
        continue;

    //do your processing
}