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
John Soer
5511 gold badge8 silver badges21 bronze badges
-
9What error message did you get?Justin Ethier– Justin Ethier2010年04月05日 16:49:51 +00:00Commented Apr 5, 2010 at 16:49
-
3Appears 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)Jakob– Jakob2010年04月05日 16:59:16 +00:00Commented Apr 5, 2010 at 16:59
2 Answers 2
DateTime.CompareTo is overloaded. Try using explicit parameter types in your lambda:
(DateTime p1, DateTime p2) => ...
answered Apr 5, 2010 at 16:53
Oliver Mellet
2,38713 silver badges10 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Oliver Mellet
Ah, didn't read that 2nd example closely enough. Anyway, if PatientPrecriptionsEntity.CompareTo is overloaded, the same comment applies.
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
BlueMonkMN
25.7k11 gold badges86 silver badges153 bronze badges
Comments
lang-cs