【问题标题】:SqlFunctions.StringConvert and Dynamic linqSqlFunctions.StringConvert 和动态 linq
【发布时间】:2016-10-30 05:10:53
【问题描述】:

我有一个 Int64 字段,我想在我的 EF Dynamic Linq 查询中进行转换。 这是因为我想使用 teh Contains 函数来检查 Int64 是否包含某个系列的数字。 所以我使用 SqlFunctions.StringConvert 之类的

SqlFunctions.StringConvert(MyField).Contains("2012")

动态库引发 ParseException:“SqlFunctions”类型中不存在适用的方法“StringConvert”。

我在动态库中更改了这个数组,以便定义 SqlFunctions:

static readonly Type[] predefinedTypes = {
        typeof(Object),
        ...
        typeof(Math),
        typeof(Convert),
        typeof(EntityFunctions), 
        typeof(SqlFunctions)
    };

奇怪的是:我还添加了 EntityFunctions,效果很好,例如:

EntityFunctions.TruncateTime(LastCommentDate) = @0

更新:SqlFunctions 不支持 Int64:

public static string StringConvert(decimal? number);
public static string StringConvert(double? number);
public static string StringConvert(decimal? number, int? length);
public static string StringConvert(double? number, int? length);
public static string StringConvert(decimal? number, int? length, int? decimalArg);
public static string StringConvert(double? number, int? length, int? decimalArg);

【问题讨论】:

    标签: linq dynamic


    【解决方案1】:

    这是我为使 SqlFunctions.StringConvert 使用 EF Dynamic Linq 查询扩展在我的项目中工作所做的工作。

    当我遇到与您相同的问题时,这是我试图开始工作的代码行,但我的“属性”是 Int32 而不是 Int64。

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

    我在 CompareConversions 方法中添加了以下代码行

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

    我猜你会想要添加如下一行来让你的代码工作

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

    我知道这看起来像一个 hack,但问题是库无法选择哪种方法

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

    更好,所以我强迫它使用 Int32 到 Decimal 的转换作为表达式,或者在你的情况下使用 Int64 到 Decimal 转换来强制它的手(以及具有相同签名的任何其他方法)。

    这里是完整的方法供参考

        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;
        }
    

    希望这会有所帮助...

    【讨论】:

      【解决方案2】:

      我认为像here 那样首先将其转换为双精度(或小数)然后进行转换会更容易(您可能会出现溢出,因为这些浮点类型无法处理与Int64)

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

      【讨论】:

      • 动态linq能识别SqlFunctions吗?
      猜你喜欢
      • 1970-01-01
      • 2014-06-02
      • 2011-08-18
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 2011-04-10
      相关资源
      最近更新 更多