1

我有一种方法可以在数据库中搜索具有相同公司名称的所有客户,然后返回他们的 ID 号列表,现在我将它们保存到文本文件中,以便我可以确认它是否有效。

我遇到的问题是访问该 ID 号列表并返回并删除它们。在下面的代码中,我使用了一个请求,该请求将通过他们的 ID 号取消客户。我使用了一个 foreach 循环,它应该获取 ID 列表并将它们全部删除,但它只是删除一个而不是每次都删除一个,它每次都删除同一个,而不是尝试,我只是得到异常说您正在尝试删除已被删除的客户。请就我做错的事情提出任何建议!

SearchRequest _request;
CancelRequest _request2;

SearchResponse _response;
CancelResponse _response2;

public void ArrangeRequest() {
    _request=new CustomerSearchRequest();
    _request.Company="Test Inc. ";
}
var customerIds=_response.Customers.Select(c => c.CustID).ToList();

foreach(var custID in customerIds) {
    _request2=new CancelRequest();
    _request2.CustID=custID;
    _request2.Company=_request.Company;
}
public void Response() {
    var ws=new RunEngine();

    _response=ws.SearchCust(new AppHeader(), _request) as SearchResponse;
    _response2=ws.CancelCust(new AppHeader(), _request2) as CancelResponse;
}
4

3 回答 3

4

您正在重用_request2字段。不要在字段中存储单个取消请求,而是使用请求列表:

List<CancelRequest> _cancelRequests;

然后创建并将所有请求添加到此列表中:

var customerIds = _response.Customers.Select(c => c.CustID);
_cancelRequests = customerIds.Select(custID => new CancelRequest { 
                          CustID = custID,
                          Company = _request.Company
                  }).ToList();

并在以后一一处理这些请求。

于 2013-03-11T15:42:38.157 回答
1

似乎您的问题不会直接发生在列表中,因为您CancelCust可以一次处理Request一个。我实际上无法理解您发布的最早的代码片段应该在哪里,所以我只是命名它NowhereMethod

我还修改了您的代码,更正了类似_request.Company=_request.Company;. 我根据您发布的代码进一步从使用情况中推断出您的所有类层次结构。

正如您所说,删除是由 完成的CancelRequest,但是,正如我上面提到的,一次只能删除Request一个,并且Request推断它仅保存有关一位客户的信息。因此,我认为您的问题可以通过重写Response方法来解决。

您仍然可以认为问题即将使它们成为列表,就像其他答案所示,这些是使用 Linq 的正确方法。尽管如此,您可能需要决定在某个类中放置正确的位置,或者以正确的方式设计一个方法来获取列表。

所以,这是我推断并试图纠正的代码;请注意,我使用字段而不是那些可能是属性,并且只放那些需要的。

您可能想查看代码中的注释。

partial class Listener /* I named it, to put your code */ {
    SearchRequest _request;
    CancelRequest _request2;

    SearchResponse _response;
    CancelResponse _response2;

    public void ArrangeRequest() {
        _request=new CustomerSearchRequest();
        _request.Company="Test Inc. ";
    }

    void NowhereMethod() {
        var customerIds=_response.Customers.Select(c => c.CustID).ToList();

        foreach(var custID in customerIds) {
            _request2=new CancelRequest();
            _request2.CustID=custID;
            _request2.Company=_request.Company;
        }
    }

    public void ResponseOriginal() {
        var ws=new RunEngine();

        _response=ws.SearchCust(new AppHeader(), _request) as SearchResponse;
        _response2=ws.CancelCust(new AppHeader(), _request2) as CancelResponse;
    }

    public void Response() /* tried to correct */ {
        var ws=new RunEngine();

        _response=ws.SearchCust(new AppHeader(), _request) as SearchResponse;

        var customerIds=_response.Customers.Select(c => c.CustID).ToList();

        foreach(var custID in customerIds) {
            _request2=new CancelRequest();
            _request2.CustID=custID;
            _request2.Company=_request.Company;

            // Seems it should be like this
            // but note the assignment might be wrong, it's according to what `CancelCust` returns
            // for the correct way to make it a list of Customer is appeared in other answers 
            _response2=ws.CancelCust(new AppHeader(), _request2) as CancelResponse;
        }
    }
}

partial class Customer {
    public String CustID;
}

partial class Response {
    public List<Customer> Customers;
}

partial class Request {
    public String Company;
    public String CustID;
}

partial class SearchResponse: Response {
}

partial class CancelResponse: Response {
}

partial class SearchRequest: Request {
}

partial class CancelRequest: Request {
}

partial class CustomerSearchRequest: SearchRequest {
}

partial class AppHeader {
}

partial class RunEngine {
    public Response SearchCust(AppHeader appHelper, Request request) {
        // I don't know what it's like
        throw new NotImplementedException();
    }

    public Response CancelCust(AppHeader appHelper, Request request) {
        // I don't know what it's like
        throw new NotImplementedException();
    }
}

RequestandCustomer可以声明为

partial class Customer {
    // Company was not appearing used in the code
    public String CustID;
}

partial class Request {
    public String Company;
    public String CustID;
}

或者

partial class Customer {
    public String Company;
    public String CustID;
}

partial class Request: Customer {
}

不会破坏代码。

于 2013-03-11T18:54:13.493 回答
0

_request似乎是一个单独的变量,而不是一个列表。然后它只会做一条记录,因为您每次都在循环中更新它,而不是将任何以前的循环值存储在列表中。

编辑:你想做这样的事情:

    var requestList = new List<CancelRequest>();
    var customerIds = _response.Customers.Select(c => c.CustID).ToList();
    foreach (var custID in customerIds)
    {
        _request = new CancelRequest(); 
        _request.CustID = custID;
        _request.Company = _request.Company;
        requestList.Add(_request);
    }
于 2013-03-11T15:42:02.460 回答