Linq to Entities: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value

StackOverflow https://stackoverflow.com/questions/16724799

سؤال

I have the following snippet that generates the error, I assume this is a result of the ConditionRatingList not having any values. Does anyone have any suggestions on how to rectify this within the linq statement?

Statement Snippet:

Dim lst = From a In _context.Assets
          Order By a.AssetIdentifier
          Select New With {
             .AssetID = a.AssetID,
             .ConditionRating = a.ConditionRatingList.OrderByDescending(Function(o) o.DateCompleted).Select(Function(o) o.Rating).FirstOrDefault,
             .ConditionRatingDate = a.ConditionRatingList.OrderByDescending(Function(o) o.DateCompleted).Select(Function(o) o.DateCompleted).FirstOrDefault}

Error Generated: "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."

هل كانت مفيدة؟

المحلول

I solved this by only getting the condition rating date if there was at least one and making sure I set the value to 1/1/1753.

.ConditionRatingDate = If(a.ConditionRatingList.Count > 0, a.ConditionRatingList.OrderByDescending(Function(o) o.DateCompleted).Select(Function(o) o.DateCompleted).FirstOrDefault, New Date(1753, 1, 1))

When I allowed the False value of the IF() to be NOTHING or Date.MinVlue I continued to have the issue, looking at the generated SQL it was translating those into;

 CASE WHEN ([Project8].[C3] > 0) THEN CASE WHEN ([Project8].[C1] IS NULL) THEN convert(datetime, '0001-01-01 00:00:00.000', 121) ELSE [Project8].[C2] END ELSE convert(datetime, '0001-01-01 00:00:00.000', 121) END AS [C15]

OR

CASE WHEN ([Project8].[C3] > 0) THEN CASE WHEN ([Project8].[C1] IS NULL) THEN convert(datetime, '0001-01-01 00:00:00.000', 121) ELSE [Project8].[C2] END ELSE '#12:00' END AS [C15]

The query now runs correctly, I will just need to educate my users as to what 1/1/1753 means.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top