需要确定List中的最小值

时间:2011-06-17 14:11:27

标签: winforms list c#-4.0 linq-to-objects

我陷入了一些看起来很容易的事情,但很快就变得头疼:

这是一个代表我正在使用的结构的类:

public class LocumJobDistanceDifferenceObject {

    public LocumJobDistanceDifferenceObject(Int64 ALocumID, Int64 AJobID, Decimal ADistanceMiles, Int32 ARateDifference, Boolean AIsDistanceUnderMax) {
        LocumID = ALocumID;
        JobID = AJobID;
        DistanceMiles = ADistanceMiles;
        RateDifference = ARateDifference;
        IsDistanceUnderMax = AIsDistanceUnderMax;
    }

    public Int64 LocumID {
        get;
        set;
    }

    public Int64 JobID {
        get;
        set;
    }

    public Decimal DistanceMiles {
        get;
        set;
    }

    public Int32 RateDifference {
        get;
        set;
    }

    public Boolean IsDistanceUnderMax {
        get;
        set;
    }
}

我创建一个List来存储信息矩阵。 Locum是一名工人,他需要被安置在工作岗位上。免得说我有50个职位和75个地点。我通过运行Locums x Jobs算法构建我的矩阵,该算法在Locum和Job +之间存储LocumID + JobID + Detrmine DistanceMiles并确定作业支付的速率/小时和Locum想要/小时+如果对Job的任务超过Locum的最大距离他/她愿意行驶

所以,基本上,因为它是Matrix中的Locums(75)x Jobs(50)行数。

现在,我需要在我的Matrix(我称之为MindMapTier01)上运行循环(ForEach),如下所示:

foreach (LocumJobDistanceDifferenceObject LocumJobDistanceDifferenceItem in MindMapTier01.OrderBy(order=>order.JobID)) {
    /**
     * Build a list (KeyValuePair<JobID, LocumID>) such that for each unique JobID,
     * I can assign the Locum closest to that Job. I need to keep in mind that
     * once a job is assigned, I dont want that JobID or LocumID for the next iteration
    **/
}

我希望自己解释一下。我需要在一两个小时内克服这个问题。请帮忙。

问候。

1 个答案:

答案 0 :(得分:0)

我不知道我完全理解您的问题,但如果您想确保将作业分配到最近的位置,那么您的代码可能如下所示:

Dictionary<Int64, Int64> dicJobLocum = New Dictionary<Int64, Int64>(); // This is the key value pair list
Dictionary<Int64, Int64> dicJobDistance = New Dictionary<Int64, Decimal>(); // This is to track the distance of the currently assigned locum

foreach (LocumJobDistanceDifferenceObject locum in MindMapTier01) {
    if (dicJobDistance.ContainsKey(locum.JobID) {
       Decimal distance = dicJobDistance(locum.JobID);
       // If the job has been assigned, check if the current locum is closer
       if (locum.DistanceMiles < distance) {
            dicJobDistance(locum.JobID) = locum.Distance;
            dicJobLocum(locum.JobID) = locum.LocumID;
       }
    }
    else {
       // If the job has not been assigned yet
       dicJobDistance.Add(locum.JobID, locum.DistanceMiles);
       dicJobLocum.Add(locum.JobID, locum.LocumID);
    }
}

请原谅任何轻微的语法错误,我最近没有使用过c#。