[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

list and linq

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?
0
JRockFL
Asked:
JRockFL
  • 6
  • 4
1 Solution
 
p_davisCommented:
yes, you can just check that list to see if it is null or count is 0

tempList = yourList.FirstOrDefault(x=> x.genericList != null && x.genericList.Count > 0)
0
 
p_davisCommented:
you can put that in a where statement... if you want straight linq
0
 
JRockFLAuthor 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();
               
            }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
p_davisCommented:
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.
0
 
JRockFLAuthor 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);
                        }
                    }
                }
            }
0
 
p_davisCommented:
o.OrderByDescending(d => d.JobScheduledDate).FirstOrDefault(x => x.Jobs != null && x.Jobs.Count > 0 && x.Jobs.Exists(j=>j.JobStatus != "C" && j.JobType != "DC"));
0
 
p_davisCommented:
getting a little convoluted now =)
0
 
JRockFLAuthor 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?
0
 
p_davisCommented:
its basically the same thing so its just a matter of preference
0
 
JRockFLAuthor Commented:
Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now