似乎您的问题不会直接发生在列表中,因为您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();
}
}
Request
andCustomer
可以声明为
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 {
}
不会破坏代码。