wootscootinboogie
wootscootinboogie

Reputation: 8695

Exception thrown in function using LINQ

I've got a simple Patient class with properties like

 public int PatientId { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }

and a function to return a random patient based on their PatientId like

 public static Patient GetRandomPatient(IEnumerable<Patient> patList)
        {
            Random r = new Random();
            int patientValue = r.Next(patList.Min().PatientId, patList.Max().PatientId);
            //return from g in patList
            //       where g.PatientId == patientValue
            //       select g;
            var test = from g in patList
                       where g.PatientId == patientValue
                       select g;
            return (Patient)test;
        }

The commented out lines are the first attempt at returning the patient whose PatientId was chosen by the Random class. That didn't compile and I was given the error Cannot implicitly convert type... (are you missing a cast)? So then I ran the iteration that isn't commented out and got the exception At least one object must implement IComparable.

So then I tried this as my return statement

 Patient testPatient = patList.First(x => x.PatientId == patientValue);
 return testPatient;

This compiles without an error, but when I run it I get the same exception that one object must implement IComparable.

I would like to know two things 1. What seems to be the concept that I'm not quite getting here with regard to returning a single object from a list using LINQ syntax (In this situation, each PatientId is unique so the return statement could only possible return a single Patient object)? 2. Why does the code

Patient testPatient = patList.First(x => x.PatientId == patientValue);
 return testPatient;

compile and give no compiler errors, but bombs with the same exception the other iterations have?

Main function

         List<Patient> patientList = new List<Patient>();
    patientList.Add(new Patient() { PatientId = 101, FirstName = "John", LastName = "Jacosdfasdfasdfb" });
                    patientList.Add(new Patient() { PatientId = 100, FirstName = "Mary", LastName = "Wilson" });
                    patientList.Add(new Patient() { PatientId=102, FirstName="Max",LastName="Payne"}); 
//Call that bombs the program Console.WriteLine(Patient.GetRandomPatient(patientList).PatientId);

Upvotes: 0

Views: 108

Answers (2)

Marcus
Marcus

Reputation: 8669

When it comes to the first statement I am curious of the rest of the error message, what types cannot be implicitly converted, my bet is that PatientId is string or nullable int (int?) while you are compating with a normal integer.

    //return from g in patList
    //       where g.PatientId == patientValue
    //       select g;

Upvotes: 0

SLaks
SLaks

Reputation: 887469

As the error is trying to tell you, your .Min() and .Max() calls don't make sense.
You can only call Min() or Max() on a collection of objects that can be compared to each-other.

Instead, you need to call them on a collection of IDs:

patients.Select(p => p.PatientId).Min()

You can also replace your entire function with the simpler (and faster)

return patients.ElementAt(rand.Next(patients.Count()));

Upvotes: 7

Related Questions