Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何根据DateTime值的接近程度将两个列表中的所有对象成对匹配_C#_.net_Algorithm_Linq - Fatal编程技术网

C# 如何根据DateTime值的接近程度将两个列表中的所有对象成对匹配

C# 如何根据DateTime值的接近程度将两个列表中的所有对象成对匹配,c#,.net,algorithm,linq,C#,.net,Algorithm,Linq,我有两个从抽象AbstractLineModel类继承的请求和响应列表。响应和请求都包含名为DateTime、MethodName的日期时间(或不包含)和GUID 调用对包含一对匹配的请求和响应: public class CallPair { public AbstractLineModel _request { get; set; } public AbstractLineModel _response { get; set; } public CallPair(A

我有两个从抽象
AbstractLineModel
类继承的
请求和
响应列表。响应和请求都包含名为
DateTime
MethodName
的日期时间(或不包含)和
GUID

调用对
包含一对匹配的
请求
响应

public class CallPair
{
    public AbstractLineModel _request { get; set; }
    public AbstractLineModel _response { get; set; }

    public CallPair(AbstractLineModel request, AbstractLineModel response)
    {
        _request = request;
        _response = response;
    }
}
CallPairOrganizer
将调用组织到以下列表中:

public class CallPairOrganizer
{
    // List of requests and matching responses
    private List<CallPair> _callPairs = new List<CallPair>();
    private List<AbstractLineModel> _lines = new List<AbstractLineModel>();
    private List<AbstractLineModel> _requests = new List<AbstractLineModel>();
    private List<AbstractLineModel> _responses = new List<AbstractLineModel>();
    private List<AbstractLineModel> _unmatchedRequests = new List<AbstractLineModel>();
    private List<AbstractLineModel> _unmatchedResponses = new List<AbstractLineModel>();
}
(2)我收集所有不匹配的请求和响应,如下所示:

public void CollectUnmatchedCalls()
{
    _unmatchedRequests = 
         _requests.Except(_callPairs.Select(cp => cp._request)).ToList();
    _unmatchedResponses = _responses.Except(_callPairs.Select(cp => 
           cp._response)).ToList();
}
这很有效。下一步是比较不匹配的请求和响应,如果它们属于相同的
MethodName
,则根据DateTime属性对它们进行排序。如果请求和响应彼此最接近,我希望将它们匹配到
调用对中

在这一部分中,我很难找出要使用什么算法/linq。我希望有人能为我提供一些见解,告诉我如何完成将请求与相同
MethodName
的响应进行匹配的任务,如果它们基于日期时间彼此接近。

那么,您需要找到与请求“最接近”的响应吗

//just some objects for me to play with
var requests =  Enumerable.Range(1).Select(i => new { Time = DateTime.Now, Method = "blah" }).ToList();
var responses =  Enumerable.Range(1).Select(i => new { Time = DateTime.Now, Method = "blah" }).ToList();

foreach (var req in requests)
{
    //In all of the responses - find the one that happened soonest after the request (with the same method name)
    var closestResponse = responses
        .Where(resp => resp.Method == req.Method)
        .OrderBy(resp => resp.Time - req.Time)
        .FirstOrDefault();

    //No more responses - exit
    if(closestResponse == null)
        break;

    responses.Remove(closestResponse);

    //make new call pair, with the closest rseponse
}

您需要花更多的时间定义匹配行为。假设你有1:00,1:15,1:25,1:40。你会将1:00与1:15匹配,因为它比其他时间更接近1:00吗?如果是这样的话,那么你仍然可以匹配1:15和1:25,或者1:15被上一场比赛“占据”了吗?有点不同,但我会用SortSet@StriplingWarrior我认为我们需要保持这种按时排序的方法作为最后手段,我需要首先找到一种匹配其他属性上的请求和响应的方法(我没有在请求和响应中发布更多信息来匹配它们)。我确实认为我会使用“采取”方法作为最后手段。没错!我会在周一回到我的代码时检查这一点。:-)代码完全符合我说的,哈哈,这是最简单的方法,我相信它会起作用。非常感谢。
//just some objects for me to play with
var requests =  Enumerable.Range(1).Select(i => new { Time = DateTime.Now, Method = "blah" }).ToList();
var responses =  Enumerable.Range(1).Select(i => new { Time = DateTime.Now, Method = "blah" }).ToList();

foreach (var req in requests)
{
    //In all of the responses - find the one that happened soonest after the request (with the same method name)
    var closestResponse = responses
        .Where(resp => resp.Method == req.Method)
        .OrderBy(resp => resp.Time - req.Time)
        .FirstOrDefault();

    //No more responses - exit
    if(closestResponse == null)
        break;

    responses.Remove(closestResponse);

    //make new call pair, with the closest rseponse
}