We help IT Professionals succeed at work.

list and linq

JRockFL
JRockFL asked
on
Medium Priority
299 Views
Last Modified: 2012-05-11
I have a generic list of WorkOrder has contains a property for a generic list of Jobs.
A WorkOrder may or may not have a list of Jobs.

I need to return the top 1 open job by date

(There might not be an open job)

Would this be something I could do in LINQ?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
you can put that in a where statement... if you want straight linq

Author

Commented:
Thank you!
How can I sort it?

I have this now...
var temp = o.FirstOrDefault(x => x.Jobs != null && x.Jobs.Count > 0);

this was from my previous code...
I was taking a list of jobs...converted to an array.

            if (jobs.Count > 0)
            {
                Job[] jobsArray = jobs.ToArray();
                var sortedJobs =
                    from d in jobsArray
                    orderby d.JobScheduledDate descending
                    select d;

                return sortedJobs.SingleOrDefault();
               
            }
CERTIFIED EXPERT

Commented:
o.OrderByDescending(d => d.JobScheduledDate).FirstOrDefault(x => x.Jobs != null && x.Jobs.Count > 0);

//sorry not sure how your JobScheduledDate is accessed, hopefully this is close.

Author

Commented:
Oops.

I also need to add this condition to this...

var temp = o.FirstOrDefault(x => x.Jobs != null && x.Jobs.Count > 0);


this was my previous code

            List<Job> jobs = new List<Job>();
            foreach (WorkOrder wo in o)
            {
                if (wo.Jobs != null)
                {
                    foreach (Job job in wo.Jobs)
                    {
                        if (job.JobStatus != "C" && job.JobType != "DC")
                        {
                            jobs.Add(job);
                        }
                    }
                }
            }
CERTIFIED EXPERT

Commented:
o.OrderByDescending(d => d.JobScheduledDate).FirstOrDefault(x => x.Jobs != null && x.Jobs.Count > 0 && x.Jobs.Exists(j=>j.JobStatus != "C" && j.JobType != "DC"));
CERTIFIED EXPERT

Commented:
getting a little convoluted now =)

Author

Commented:
lol, yes it is.
thank you for your help.

should i just keep it the "non linq" way?
what benefit do i have trying to do it with linq?
CERTIFIED EXPERT

Commented:
its basically the same thing so its just a matter of preference

Author

Commented:
Thank you!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.