我有一种方法,在数据库中搜索具有相同公司名称的所有客户,然后返回其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;
}
答案 0 :(得分: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();
稍后逐个处理这些请求。
答案 1 :(得分:1)
似乎您的问题不会直接发生在列表中,因为您的CancelCust
一次只能占用一个Request
。我实际上无法理解你发布的最早的代码片段应该在哪里,所以我只需将它命名为NowhereMethod
!
我还修改了您的代码,更正了_request.Company=_request.Company;
之类的内容。我根据您发布的代码进一步推断出所有类的层次结构。
如您所述,删除操作由CancelRequest
完成,但正如我上面提到的,一次只能使用一个Request
,并推断出Request
仅保存有关一个客户的信息。因此,我认为您的问题可以通过重写Response
方法来解决。
您仍然可以认为问题是要将它们列为其他答案所显示的列表,这些是使用Linq的正确方法。然而,您可能需要决定放入某个类的正确位置,并以正确的方式设计方法以获取列表。
所以,这是代码,我推断并试图纠正;请注意,我使用 fields 而不是那些可能是属性,并且只放置那些是必需的。
您可能想要查看代码中的注释。
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();
}
}
Request
和Customer
可以声明为
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 {
}
不会破坏代码。
答案 2 :(得分: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);
}