我有两个清单Requests
and Responses
谁继承了抽象AbstractLineModel
班级。响应和请求都包含一个(或不包含)名为RequestId
.
A CallPair
包含一对匹配的Request
and a Response
并具有以下构造函数:
public AbstractLineModel _request { get; set; }
public AbstractLineModel _response { get; set; }
public CallPair(AbstractLineModel request, AbstractLineModel response)
{
_request = request;
_response = response;
}
我想做一个List
of CallPairs
哪里的Request
与相同的 IdResponse
都匹配在Callpair
。如何匹配列表Requests
and Responses
?
我尝试的是:
public void MatchCallPairs()
{
// Finds request that have matching response
var requestWithMatches = _requests
.Where(req => _responses.All(res => res.RequestId == req.RequestId)).ToList();
// Match the request to the response
foreach (var req in requestWithMatches)
{
var resp = _responses.Where(res => res.RequestId == req.RequestId).FirstOrDefault();
// And create the pairs
_callPairs.Add(new CallPair(req, resp));
}
}
但这会返回一个空列表。
具体来说,var requestWithMatches = _requests
.Where(req => _responses.All(res => res.RequestId == req.RequestId)).ToList();
返回一个空的 requestsWithMatches 列表。
有人可以帮我解决这个算法吗?
只需在 RequestId 上加入两个序列并从加入的项目中创建 CallPair:
var requestWithMatches = from req in _requests
join resp in _responses
on req.RequestId equals resp.RequestId
select new CallPair(req, resp);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)