在循环中按LastIndex搜索列表。

时间:2015-05-04 02:09:27

标签: c# list

我正在尝试完成一个算法,将ProcTime添加到最多两个其他最大值(JobNumMax和WSMax)。我在循环中使用FindLastIndex和FindLast时遇到问题。 这是我的代码。

public class JobListOrder
        {
            public int JobNum { get; set; }
            public string Workstation { get; set; }
            public int Sequence { get; set; }
            public int ProcTime { get; set; }
            public int EndHour { get; set; }
            public DateTime DueDate { get; set; } 
            public int Priority { get; set; } 
        }

列入清单。

//New List
            List<JobListOrder> list = new List<JobListOrder>();
            using (StreamReader sr = new StreamReader("C:\\Users\\Nathan\\Documents\\Visual Studio 2013\\Projects\\PubsExample\\PubsExample\\JobsList.txt"))
            {
                //Add .txt to List
                while (sr.Peek() >= 0)
                {
                    string str;
                    string [] strArray;
                    str = sr.ReadLine();

                    strArray = str.Split(',');
                    JobListOrder currentjob = new JobListOrder();
                    currentjob.JobNum = int.Parse(strArray[0]);
                    currentjob.Workstation = strArray[1];
                    currentjob.Sequence = int.Parse(strArray[2]);
                    currentjob.ProcTime = int.Parse(strArray[3]);
                    currentjob.EndHour = int.Parse(strArray[4]);
                    currentjob.DueDate = DateTime.Parse(strArray[5]);
                    currentjob.Priority = int.Parse(strArray[6]);
                    list.Add(currentjob);

                }

按特定方式排序以开始计算

 //Job Sort
                var ListSort = from jobsort in list
                               orderby jobsort.Sequence ascending, jobsort.Priority descending, jobsort.DueDate ascending, jobsort.JobNum ascending
                               select jobsort;
                List<JobListOrder> SortList = new List<JobListOrder>(ListSort);

这是一次轻微的尝试

//foreach (var i in SortList)
                //{
                //    if (JobNumMax >= WSMax)
                //    {
                //        return i.EndHour = JobNumMax + i.ProcTime;
                //    }
                //    else
                //        return i.EndHour = WSMax + currentjob.ProcTime;
                //    for (var j = 0; j < SortList.Count; j++)
                //    {
                //        int JobLNumMaxIndex = SortList.FindLastIndex(i.JobNum)
                //        int JobNumMax = i.EndHour[JobNumMaxIndex];
                //        for (var k = 0; k < SortList.Count; k++)
                //        {
                //            int WSMaxIndex = SortList.FindLastIndex(i.Workstation);
                //            int WSMax = i.EndHour[JobNumMaxIndex];

                //        }
                //    }

                //}

我正在尝试查找查询的LastIndex并返回该特定索引的值。我将尝试在下面的代码中解释我的意思搜索JobNum = 1和Workstation =使用ProcTime为1进行铣削

            foreach (var i in SortList) //Iterate through SortList
            {
                if (JobNumMax (3) >= WSMax (4)) 
                {
                    return i.EndHour = JobNumMax (3) + i.ProcTime (1); //assigns calculation to EndHour of current record
                }
                else
                    return i.EndHour = WSMax (4) + i.ProcTime (1);
                for (var j = 0; j < SortList.Count; j++)
                {
                    int JobLNumMaxIndex = SortList.FindLastIndex(1) //Finds last record with JobNum = 1
                    int JobNumMax = i.EndHour[JobNumMaxIndex];//Return what EndHour is at the index from JobNumMaxIndex search// Lets say 3
                    for (var k = 0; k < SortList.Count; k++)
                    {
                        int WSMaxIndex = SortList.FindLastIndex(Milling);//Finds last record with Workstation = Milling
                        int WSMax = i.EndHour[JobNumMaxIndex];//Return what EndHour is at the index from WSMaxIndex search// Lets say 4

                    }
                }

            }

结果将是4 + 1 = 5.

我遇到了算法语法的问题。我无法让FindLast工作。

1 个答案:

答案 0 :(得分:1)

看起来您可能只是遇到LINQ语法问题。

FindLastIndexPredicate<JobListOrder>作为参数,即以JobListOrder作为输入的函数返回true或false。

所以代替SortList.FindLastIndex(i.JobNum)你应该有类似的东西:

SortList.FindLastIndex(order => order.JobNum == i.JobNum);

在您的代码中进行了更正:

int JobNumMax = 0;
int WSMax  0;

foreach (var i in SortList)
{
    if (JobNumMax >= WSMax)
    {
        return i.EndHour = JobNumMax + i.ProcTime;
    }
    else if (JobNumMax > 0 && WSMax > 0)
    {
        return i.EndHour = WSMax + currentjob.ProcTime;
    }

    for (var j = 0; j < SortList.Count; j++)
    {
        int JobLNumMaxIndex = SortList.FindLastIndex(order => order.JobNum == i.JobNum);
        JobNumMax = i.EndHour[JobNumMaxIndex];

        for (var k = 0; k < SortList.Count; k++)
        {
            int WSMaxIndex = SortList.FindLastIndex(order => order.Workstation == i.Workstation);
            WSMax = i.EndHour[JobNumMaxIndex];
        }
    }
}