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.

2
  • 9
    What error message did you get? Commented Apr 5, 2010 at 16:49
  • 3
    Appears to compile fine for me. What type is tvPatientPrecriptionsEntities? (And is it correct to assume that p1.MedicationStartDate is a nullable datetime? ("DateTime?" that is) Commented Apr 5, 2010 at 16:59

2 Answers 2

2

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

(DateTime p1, DateTime p2) => ...
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, didn't read that 2nd example closely enough. Anyway, if PatientPrecriptionsEntity.CompareTo is overloaded, the same comment applies.
1

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.