IEnumerable LINQ Query SmartDate issue

IEnumerable LINQ Query SmartDate issue

Old forum URL: forums.lhotka.net/forums/t/11263.aspx


BruderKotlett posted on Wednesday, March 28, 2012

Hello,

I've an issue regarding LINQ and an IEnumerable collection with BusinessBase objects containing SmartDate properties. Using the latest CSLA.NET Release.

Here is what I try (hopefully enough code):

IEnumerable collection = ItemsSource as IEnumerable; // Collection of Properhandels Objects

// query.FilterString --> "ValidUntil != null AND ValidUntil > @0"
// query.QueryParameters --> [0] DateTime 01.03.2012

var result = collection.AsQueryable().Where(query.FilterString, query.QueryParameters.ToArray<object>());

namespace System.Linq.Dynamic
{
    public static class DynamicQueryable
    {
        void CheckAndPromoteOperands(Type signatures, string opName, ref Expression left, ref Expression right, int errorPos)
        {
            Expression[] args = new Expression[] { left, right };
            MethodBase method;
            if (FindMethod(signatures, "F", false, args, out method) != 1)
                throw IncompatibleOperandsError(opName, left, right, errorPos); // <-- ParseException
            left = args[0];
            right = args[1];
        }
    }
}


And I receive this ParseException
Operator '!=' incompatible with operand types 'SmartDate' and 'Object'
Anyone who can help me with this issue? Is there something I do wrong? With an DateTime property everything works fine.
Kind Regards
Kevin

 

tmg4340 replied on Wednesday, March 28, 2012

Part of what you're running into is that a SmartDate is declared as a struct.  Therefore, by definition it can't be NULL.

More specific to your error, the SmartDate struct has several operator overloads, but none of them take an object as the second argument.  Since a NULL value doesn't convert to anything other than an object type without a specific cast, .NET can't find an operator overload with the correct signature.

My best recommendation for this error is to remove the NULL check from your query filter, as it shouldn't be necessary.

HTH

- Scott

JonnyBee replied on Wednesday, March 28, 2012

My general recommendation is to NOT expose the SmartDate struct as a property.

Use snippet cslapropdt to define the property.

    public static readonly PropertyInfo<SmartDate> StartDateProperty = RegisterProperty<SmartDate>(c => c.StartDate);
    public string StartDate
    {
      get { return GetPropertyConvert<SmartDatestring>(StartDateProperty); }
      set { SetPropertyConvert<SmartDatestring>(StartDateProperty, value); }
    }

If necessary add a when you also need the DateTime value

    [Browsable(false)]     public DateTime StartDateAsDateTime     {         get { return GetPropertyConvert<SmartDateDateTime>(StartDateProperty); }         set { SetPropertyConvert<SmartDateDateTime>(StartDateProperty, value); }     }

Copyright (c) Marimer LLC