- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这让我很困惑。我知道 LINQ-to-SQL 通过处理表达式树并尝试通过生成的查询来翻译内容来处理选择,这就是为什么某些函数翻译不起作用的原因( string.IsNullOrWhitespace
是一个常见的烦恼)。
我在我的代码中遇到了 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()
时.
我已经尝试了几种不同的方法名称来尝试确定一种模式,但未能成功。
看起来如果方法被命名为以 To
开头的任何内容它会抛出 NotSupportedException
, 但似乎可以使用任何其他名称。这仍然很奇怪。
谁能解释一下发生了什么?
这是另一个没有 #define
的示例切换,它只是背靠背地执行两种方法,我仍然看到同样的问题。具体来说,DoIt
有效,但是 ToValueType
没有。
using System;
using Sandbox.Data;
using System.Linq;
namespace Sandbox.Console
{
class Program
{
static void Main(string[] args)
{
using (var dataContext = new SandboxDataContext())
{
try
{
var myValueList = dataContext.Numbers.Select(x => new MyValue
{
Type = DoIt(x.Value),
}).ToList();
System.Console.WriteLine("DoIt Succeeded, found {0} results", myValueList.Count);
}
catch (NotSupportedException)
{
System.Console.WriteLine("DoIt Failed, oh noes!");
}
try
{
var myValueList = dataContext.Numbers.Select(x => new MyValue
{
Type = ToValueType(x.Value),
}).ToList();
System.Console.WriteLine("ToValueType Succeeded, found {0} results", myValueList.Count);
}
catch (NotSupportedException)
{
System.Console.WriteLine("ToValueType Failed, oh noes!");
}
System.Console.ReadKey();
}
}
public static MyValueType DoIt(int value)
{
return MyValueType.SpecialType;
}
public static MyValueType ToValueType(int value)
{
return MyValueType.SpecialType;
}
public sealed class MyValue
{
public MyValueType Type { get; set; }
}
public enum MyValueType
{
SpecialType,
}
}
}
最佳答案
这是一个 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被放弃。
关于c# - 为什么 LINQ-to-SQL 有时允许我使用函数进行投影,但有时却不允许?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34600474/
每当我设置 Border.Effect属性的投影效果 控件中包含的每个控件都有一个投影。 有没有办法将阴影设置到边框而不是边框中包含的每个控件? 这是我的代码的一个简短示例:
我正在尝试创建一个带有阴影的对象。我相信您需要 CSS3 来执行此操作,到目前为止我已经有了类似的东西。 div { width:300px; height:100px; background
我希望能够为我的 drawables 文件夹中的矢量添加阴影。目前,当我将具有阴影的 svg 导入 Android Studio 时,转换为 xml 会删除阴影。 我将如何在 Android Stud
使用 NHibernate 2.1,我试图将一个实体及其子集合投影到 DTO 中。我的实体看起来像这样.. public class Application { public int Id {ge
我有问题还是好,我不知道如何将x,y,z值的3d点转换为2d点, 我必须绘制投影,其中的点确实有x,y,z值,但是我不知道如何将它们转换为2d,所以我可以将它们移动到我的轴上。 我一直在寻找Wiki和
我有域类位置 public abstract class BaseEntity where T: struct { public virtual T Id { get; set
我有一个使用 Android Material 图标作为背景的 ImageView 。我尝试添加标高以创建投影效果,但仅显示图标。是否可以将标高添加到矢量资源可绘制对象中? 矢量代码:
我正在尝试连接并以逗号分隔(或空格)列表并将其投影。我在下面有一些示例代码。 public class Friend { public string Name { get; set; } }
是否有任何库可以轻松地允许 Java bean 投影? 我有一个按照 Javabean 约定用 getter 和 setter 编写的 bean,并且在运行时在不同的地方我想要获取一个完全填充的 be
我可以成功地做到: point.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:
我对 OpenLayers 3 有点问题。我有以下脚本: var map = new ol.Map({ view : new ol.View({ center : [5.611
我正在尝试向 ImageView 添加阴影。 Stackoverflow 的另一个答案似乎是使用 Canvas 和位图等,比需要的复杂得多。 在 iOS 上我会做这样的事情: myImageView.
我有一个 JPanel 元素,我想给它添加一个阴影,我怎样才能给元素添加一个漂亮的褪色阴影?我需要使用外部库还是可以使用内置的东西? 例子: 最佳答案 所以我查看了 swingx它扩展了 JPanel
如何使用 MongoDB 查询预测作者的名字 { name: "Wings Of Fire", author: { first: "Abdul", last: "Kalam" } }
我有一个集合“帐户”,其中包含类似于此结构的文档: { "email" : "john.doe@acme.com", "groups" : [ {
我试图弄清楚如何使用枚举列表(@ElementCollection)对实体进行 DTO 投影。不幸的是,缺少 QueryDsl 文档,在这里我只能找到版本 3 的结果 不是 适用于版本 4。 @Ent
我想要悬停 div 时出现的箭头 here也投下影子。箭头是从 CSS 绘制的: .arrow { position:absolute; margin-top:-50px; left:80px; bo
如何使用 QueryOver 和 AliasToBean 将枚举值转换为字符串值?我有以下但在尝试转换 Enum 时出错: SomeDTO someDTO = null;
我有这个外部 GeoJSON 文件: {"type": "FeatureCollection", "features": [ {"type":"Feature", "id":382, "propert
我对 Hibernate 的预测和标准有点困惑。何时使用预测以及何时使用标准? 最佳答案 它们并不相互排斥,您可以同时使用两者。预测通常在某些标准的背景下使用。 简单地说,Hibernate Proj
我是一名优秀的程序员,十分优秀!