Tim
Tim

Reputation: 952

How to specify which object in an ICollection you want to pull out

I have an ICollection of CurJob objects. I need to access properties from a specific one of those objects. The way that I am doing it now just gives me access to the first object in the collection. I know that I am telling it to use .First() and I'm realizing that may not be what I want. So basically there could only be 1 in this collection or there could be an infinite number. I want to be able to grab the specific one I need based on a property called entryNumber. I need the highest entryNumber to be the object that I am exposing. Not sure how to handle thought. Any thoughts?

// Find the job we just submitted
CurJob runningJob = CurJob.Find("Some Job", StateType.Any, server).ToList().First();

Upvotes: 0

Views: 74

Answers (1)

Connell.O'Donnell
Connell.O'Donnell

Reputation: 3723

You almost have it. Just order by entryNumber before picking the first one.

CurJob job = CurJob.Find("Some Job", StateType.Any, server).ToList()
                   .OrderByDescending(j => j.entryNumber).First();

Edit

You'll need to use the System.Linq namespace for this.

Edit

If you're looking for all the jobs that match a specific entryNumber, not the highest, try something like this:

var jobs = CurJob.Find("Some Job", StateType.Any, server).ToList()
                   .Where(j => j.entryNumber == X);

(Replace X with the number you're looking for)

Upvotes: 2

Related Questions