List and Linq

I'm trying to return the most recent job.
In this example, I would expect to return the job with job number 100.
How can I sort desc?


        public void TestMethod1()
        {
            List<WorkOrder> o = GetWorkOrders();

            var tempList =  o.FirstOrDefault(x => x.Jobs != null && x.Jobs.Count > 0
                && x.Jobs.Exists(j => j.JobStatus != "C"));            

            Job job = ToBusiessObect(tempList);

            if (job != null)
            {
                System.Diagnostics.Debug.Write(job.JobNumber);
            }

        }

        public class WorkOrder
        {
            public string WorkOrderNumber { get; set; }
            public List<Job> Jobs { get; set; }
        }

        public class Job
        {
            public string JobNumber { get; set; }
            public string JobType { get; set; }
            public string JobStatus { get; set; }
            public DateTime JobDate { get; set; }
        }

        public List<WorkOrder> GetWorkOrders()
        {
            List<WorkOrder> list = new List<WorkOrder>();

            WorkOrder wo1 = new WorkOrder();
            wo1.WorkOrderNumber = "1234";

            WorkOrder wo2 = new WorkOrder();
            wo2.WorkOrderNumber = "1235";
            Job job1 = new Job();
            job1.JobDate = new DateTime(2011, 4, 15);
            job1.JobNumber = "55";
            job1.JobStatus = "O";
            job1.JobType = "AP";

            List<Job> jobList = new List<Job>();
            jobList.Add(job1);

            wo2.Jobs = jobList;

            list.Add(wo2);

            WorkOrder wo3 = new WorkOrder();
            wo3.WorkOrderNumber = "1236";
            List<Job> joblist2 = new List<Job>();
            Job job3 = new Job();
            job3.JobDate = new DateTime(2011, 4, 29);
            job3.JobNumber = "100";
            job3.JobStatus = "O";
            job3.JobType = "AP";

            joblist2.Add(job3);
            wo3.Jobs = joblist2;

            list.Add(wo3);
            return list;
        }

        internal Job ToBusiessObect(WorkOrder o)
        {
            if (o == null) return null;

            return o.Jobs.SingleOrDefault();
        }
LVL 8
JRockFLAsked:
Who is Participating?
 
jhshuklaCommented:
@JRock,
Why do you have null job in the list? if there is nothing to do, why not have an empty list?

No kaufmed.
o.Where(...) will give you IEnumerable<WorkOrder>.
the variable j in OrderByDescending is of type WorkOrder, it does not have JobNumber field.
JobNumber is a string and casting it to int will generate exception.

var latestJob = o
.SelectMany(x => x.Jobs)                      // flatten WorkOrder list into Job list
.Where(j => j != null && j.JobStatus != "C")  // filter out the ones that are null or cancelled
.OrderByDescending(j => j.JobDate)            // sort desc by date. use JobNumber if that's what you want.
.FirstOrDefault();                            // the most recent.

Open in new window

0
 
jhshuklaCommented:
1. flatten the work order list into job list. see documentation on SelectMany.
2. sort by date. descending.
3. take the first job.
0
 
jhshuklaCommented:
sort descending using orderbydescending method.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
JRockFLAuthor Commented:
Thank you for your reply. I'm doing this in small steps.
One of the jobs is null, so I get an exception.

How can I take get the the jobs that are not null?

            IEnumerable<Job> selectedJobs = o.SelectMany(jobs => jobs.Jobs);

            foreach (Job j in selectedJobs)
            {
                System.Diagnostics.Debug.WriteLine(j.JobNumber);
            }
0
 
käµfm³d 👽Commented:
How about this?
var tempList = o.Where(x => x.Jobs != null && x.Jobs.Count > 0 && x.Jobs.Any(j => j.JobStatus != "C")
                .OrderByDescending(j => (int)j.JobNumber)
                .FirstOrDefault();

Open in new window

0
 
JRockFLAuthor Commented:
kaufmed,

I get a compiler error
Please see the screen shot.

 screen shot
0
 
JRockFLAuthor Commented:
@jhshukla,

There are nulls because that is what is returned from our the web service we use.
Our existing code follows this, so I would not be able to create an empty list.

Your sample works if the are no nulls for job.
If there is null, then there is null reference.
See the screen shot. Screen shot
0
 
JRockFLAuthor Commented:
I added a null check for the job number and it is now working.
Any objections to that?
var latestJob = o
                .SelectMany(x => x.Jobs)                                                // flatten WorkOrder list into Job list
                .Where(j => j != null && j.JobNumber != null && j.JobStatus != "C")     // filter out the ones that are null or cancelled
                .OrderByDescending(j => j.JobDate)                                      // sort desc by date. use JobNumber if that's what you want.
                .FirstOrDefault();                                                      // the most recent.

Open in new window

0
 
käµfm³d 👽Commented:
JobNumber is a string and casting it to int will generate exception.
So it does. I didn't realize there was a Date field there anyway. I would have sorted by that had I saw it  = )
0
 
JRockFLAuthor Commented:
Thank you for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.