tags:

views:

122

answers:

2

I am very confused.

I have this lambda expression:

tvPatientPrecriptionsEntities.Sort((p1, p2) =>
    p1.MedicationStartDate
      .Value
      .CompareTo(p2.MedicationStartDate.Value));

Visual Studio will not compile it and complains about syntax.

I converted the lamba expression to an anonymous delegate as so:

tvPatientPrecriptionsEntities.Sort(
  delegate(PatientPrecriptionsEntity p1, PatientPrecriptionsEntity p2) 
  {
      return p1.MedicationStartDate
               .Value
               .CompareTo(p2.MedicationStartDate.Value);
  });

and it works fine.

The project uses .NET 3.5 and I have a reference to System.Linq.

A: 

DateTime.CompareTo is overloaded. Try using explicit parameter types in your lambda:

(DateTime p1, DateTime p2) => ...
omellet
These aren't DateTimes, but rather a custom class...
Reed Copsey
Ah, didn't read that 2nd example closely enough. Anyway, if PatientPrecriptionsEntity.CompareTo is overloaded, the same comment applies.
omellet
+1  A: 

The following code compiles fine for me. Perhaps you should narrow down what significant differences exist between your code, and this simple example to pin down the source of the problem.

static void Main(string[] args)
{
   PatientPrescriptionsEntity[] ppe = new PatientPrescriptionsEntity[] {};
   Array.Sort<PatientPrescriptionsEntity>(ppe, (p1, p2) => 
       p1.MedicationStartDate.Value.CompareTo(p2.MedicationStartDate.Value));
}
...
class PatientPrescriptionsEntity
{
   public DateTime? MedicationStartDate;
}
BlueMonkMN