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.

Jeff Yates
62.6k20 gold badges144 silver badges193 bronze badges
asked Apr 5, 2010 at 16:47
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) => ...
answered Apr 5, 2010 at 16:53
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;
}
answered Apr 5, 2010 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.