SqlFunctions.StringConvert and Dynamic linq

为君一笑 提交于 2019-12-08 19:52:28

Here is what I did to get SqlFunctions.StringConvert to work in my project using the EF Dynamic Linq query extensions.

Here is a the line of code I was trying to get to work when I ran into the same issue you where having but my "Property" was an Int32 not Int64.

query.Where("SqlFunctions.StringConvert(Property).Contains(\"12\")");

I added the following line of code to the CompareConversions method

if (s == typeof (Int32) && t1 == typeof (Decimal?)) return 1;

I would guess that you would want to add a line like the following to get your code to work

if (s == typeof (Int64) && t1 == typeof (Decimal?)) return 1;

I know this looks like a hack and it is, but the issue is the library can not choose which method

public static string StringConvert(decimal? number);
public static string StringConvert(double? number);

is better, so I forced its hand (and any other method with the same signature) by making it use the conversion of Int32 to Decimal for the Expression, or in your case a Int64 to Decimal conversion.

Here is the completed method for reference

    private static int CompareConversions(Type s, Type t1, Type t2)
    {
        if (t1 == t2) return 0;
        if (s == t1) return 1;
        if (s == t2) return -1;
        bool t1t2 = IsCompatibleWith(t1, t2);
        bool t2t1 = IsCompatibleWith(t2, t1);
        if (t1t2 && !t2t1) return 1;
        if (t2t1 && !t1t2) return -1;
        if (IsSignedIntegralType(t1) && IsUnsignedIntegralType(t2)) return 1;
        if (IsSignedIntegralType(t2) && IsUnsignedIntegralType(t1)) return -1;
        // HACK: SqlFunctions.StringConvert - Force it to think the Decimal Signature
        // is better than the double for Int32
        if (s == typeof (Int32) && t1 == typeof (Decimal?)) return 1;
        return 0;
    }

Hope this helps...

Daniël Tulp

I think it would be easier to first cast it to double (or decimal) as they do here and then do the conversion (you can have a possible overflow because these float types cannot handle the same amount of values as an Int64)

query.Where("SqlFunctions.StringConvert((double)Property).Contains(\"12\")");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!