Hi Guys,
We have a number of fields in the DB that are persisted in the DB as strings and are used converted to enums using LightSpeeds IFieldConverter, eg...
public class StatusType
{
internal class StatusTypeConverter : IFieldConverter
{
public object ConvertFromDatabase(object databaseValue)
{
try
{
return Enum.Parse(typeof(Status), (string)databaseValue, true);
}
catch
{
return Status.Open;
}
}
public object ConvertToDatabase(object value)
{
return ((Status) value).ToString();
}
}
}
This works well most of the time.
But have I done something wrong somewhere, or do I need to do something else as well? As I am getting errors when trying to GroupBy a status field...
TableWithStatusFields.Where(b=>b.UpdatedOn > DateTime.Today.AddDays(-30)).GroupBy (b => b.Status).Select (b => new { Status = b.Key.ToString(), Count = b.Count() });
Creates this SQL...
SELECT
g1.Status
FROM
(
SELECT
TableWithStatusFields.Status AS [Status]
FROM
TableWithStatusFields TableWithStatusFields
WHERE
(TableWithStatusFields.UpdatedOn > '15/05/2015 00:00:00' AND TableWithStatusFields.DeletedOn IS NULL)
GROUP BY
TableWithStatusFields.Status
)
g1
And results in the following error...
TargetSite : Number.StringToNumber (String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
StackTrace
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Mindscape.LightSpeed.Linq.Utils.TypeHelper.NonNullableConvert(LightSpeedContext context, Object value, Type targetType)
at Mindscape.LightSpeed.Linq.Utils.TypeHelper.NullableSafeConvert(LightSpeedContext context, Object value, Type targetType)
at Mindscape.LightSpeed.Linq.Plan.GroupResultsPlan.ExecuteImmediate(IUnitOfWork unitOfWork, Type returnType)
at Mindscape.LightSpeed.Linq.LinqQueryProvider.Execute(Expression expression)
at Mindscape.LightSpeed.Linq.LinqQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression)
at Mindscape.LightSpeed.Linq.LinqQuery`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at UserQuery
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Do I need to implement something else, or does something need to be fixed?
Cheers,
Sean