为什么LINQ-to-SQL有时允许我使用函数进行投影,但有时它不会?

时间:2016-01-04 21:47:38

标签: c# linq-to-sql

这真让我感到困惑。我知道LINQ-to-SQL处理选择是通过处理表达式树并尝试通过生成的查询翻译东西,这就是为什么某些功能翻译不起作用属性(string.IsNullOrWhitespace是一个常规的烦恼)。 / p>

我遇到了我的代码中的情况,其中LINQ-to-SQL能够通过Select投影调用我的帮助函数...有时候。实际上,当方法有一个名称时,它可以工作,但不能使用它有另一个名称。我认为这可以通过以下程序得到最好的说明(这很简单,我可以做到):

// #define BREAK_THE_CODE

using System;
using Sandbox.Data;
using System.Collections.Generic;
using System.Linq;

namespace Sandbox.Console
{

    class Program
    {
        static void Main(string[] args)
        {
            using (var dataContext = new SandboxDataContext())
            {
                List<MyValue> myValueList;

                try
                {
                    myValueList = dataContext.Numbers.Select(x => new MyValue
                    {
#if BREAK_THE_CODE
                        Type = ToValueType(x.Value),
#else
                        Type = DoIt(x.Value),
#endif
                    }).ToList();
                }
                catch (NotSupportedException)
                {
                    System.Console.WriteLine("Not supported, oh noes!");
                    System.Console.ReadKey();
                    return;
                }

                System.Console.WriteLine(myValueList.Count);
                System.Console.ReadKey();
            }
        }

#if BREAK_THE_CODE
        public static MyValueType ToValueType(int value)
#else
        public static MyValueType DoIt(int value)
#endif
        {
            return MyValueType.One;
        }

        public sealed class MyValue
        {
            public MyValueType Type { get; set; }
        }

        public enum MyValueType
        {
            One,
            Two,
            Unknown,
        }
    }
}

支持数据库只有一个名为[Number]的表,其中包含一列[Value] INT NOT NULL

如上所述,该程序适用于我,但取消注释顶部的#define将切换方法调用的名称,然后程序将在执行NotSupportedException时抛出ToList()

我尝试了几个不同的方法名称来尝试确定一个模式,但是还没能。看起来该方法的名称是以{{1}开头的任何名称它将抛出To,但似乎与任何其他名称一起使用。这仍然很奇怪。

有人可以解释发生了什么吗?

这是另一个没有NotSupportedException切换的示例,它只是两种方法背靠背,我仍然看到同样的问题。具体来说,#define有效,但DoIt没有。

ToValueType

1 个答案:

答案 0 :(得分:4)

这是一个L2S错误。预计这将有效。

在反编译的源代码中,我找到:

    private static MethodSupport GetDecimalMethodSupport(SqlMethodCall mc)
    {
        if (mc.Method.IsStatic)
        {
            if (mc.Arguments.Count == 2)
            {
                string str;
                if (((str = mc.Method.Name) != null) && ((((str == "Multiply") || (str == "Divide")) || ((str == "Subtract") || (str == "Add"))) || ((str == "Remainder") || (str == "Round"))))
                {
                    return MethodSupport.Method;
                }
            }
            else if (mc.Arguments.Count == 1)
            {
                string str2;
                if (((str2 = mc.Method.Name) != null) && (((str2 == "Negate") || (str2 == "Floor")) || ((str2 == "Truncate") || (str2 == "Round"))))
                {
                    return MethodSupport.Method;
                }
                if (mc.Method.Name.StartsWith("To", StringComparison.Ordinal))
                {
                    return MethodSupport.Method;
                }
            }
        }
        return MethodSupport.None;
    }

正在搜索"To"

if (mc.Method.Name.StartsWith("To", StringComparison.Ordinal))
嗯......还有一个地方。也许L2S认为你的价值类型是小数。尝试添加第二个参数以打破mc.Arguments.Count == 1条件。

无论如何,这是一个错误。这背后没有更深层的原因。通常,L2S可以在最终的Select中执行函数。这是EF仍然缺乏的一个很棒的功能。

将其放置休息并尽快迁移到EF。 L2S被放弃了。