问题描述
我不知道是否有人知道确切如果LINQ到SQL具有生成TSQL代码包含 ISNULL
函数的能力?
我知道,使用聚结操作符( ??
)中的查询:
邻表
,其中(o.Field ?? 0)GT; 0
选择o
将导致LINQ to SQL来发出 COALESCE
功能:
SELECT [T0] [现场]
从[表] AS [T0]
WHERE(COALESCE([T0] [现场],0))> 0
和,在使用条件运算符(:$ C在查询$ C>):
邻表
,其中(邻点域== NULL?0:o.Field)GT; 0
选择o
将导致TSQL包含 CASE
语句:
SELECT [T0] [现场]
从[表] AS [T0]
WHERE(
(CASE
当[T0]。[现场]为null,则0
ELSE [T0]。[金额]
端))> 0
不过,可以LINQ到SQL裹挟到包含生成TSQL代码 ISNULL
像下面这样?
SELECT [T0]。[现场]
FROM [表] AS [T0 [
,其中(ISNULL([T0] [技术领域],0)。)> 0
我打赌的回答是不,这不可能,但我会。喜欢看一些权威
我知道实现这个是通过你自己的类,像这样的唯一方法:
公共部分类LocalTestDataContext
{
[功能(NAME =ISNULL,IsComposable = TRUE)]
[返回:参数(的DbType =的NVarChar(MAX))]
公共字符串ISNULL(
[参数(名称=场,的DbType =的NVarChar(MAX))]串场,
[参数(名称=输出的DbType =的NVarChar(MAX))]字符串输出)
{
回报率((字符串)(this.ExecuteMethodCall(这一点,
((MethodInfo的)(MethodInfo.GetCurrentMethod())),
场,输出).ReturnValue));
}
}
这是拿#3从此处。一>
VAR CTX =新LocalTest.LocalTestDataContext();
VAR的查询=从C在ctx.Categories
排序依据ctx.IsNull(c.Description1,)+ ctx.IsNull(c.Description2,)
选择C;
query.Dump();
和将产生的T-SQL中使用ISNULL()。
I was wondering whether anyone knows definitively if LINQ to SQL has the capability of generating TSQL code that contains the ISNULL
function?
I'm aware that using the coalesce operator (??
) in a query:
from o in Table
where (o.Field ?? 0) > 0
select o
will cause LINQ to SQL to emit the COALESCE
function:
SELECT [t0].[Field]
FROM [Table] AS [t0]
WHERE (COALESCE([t0].[Field],0)) > 0
And, that using the conditional operator (?:
) in a query:
from o in Table
where (o.Field == null ? 0 : o.Field) > 0
select o
will result in TSQL containing a CASE
statement:
SELECT [t0].[Field]
FROM [Table] AS [t0]
WHERE (
(CASE
WHEN [t0].[Field] IS NULL THEN 0
ELSE [t0].[Amount]
END)) > 0
But, can LINQ to SQL be coerced into generating TSQL code that contains ISNULL
like the following?
SELECT [t0].[Field]
FROM [Table] AS [t0]
WHERE (ISNULL([t0].[Field],0)) > 0
I'm betting the answer is "no, it can't," but I'd like to see something authoritative.
The only way I know of accomplishing this is via your own class like so:
public partial class LocalTestDataContext
{
[Function(Name = "IsNull", IsComposable = true)]
[return: Parameter(DbType = "NVarChar(MAX)")]
public string IsNull(
[Parameter(Name = "field", DbType = "NVarChar(MAX)")] string field,
[Parameter(Name = "output", DbType = "NVarChar(MAX)")] string output)
{
return ((string)(this.ExecuteMethodCall(this,
((MethodInfo)(MethodInfo.GetCurrentMethod())),
field, output).ReturnValue));
}
}
This is under "Take #3" from here.
var ctx = new LocalTest.LocalTestDataContext();
var query = from c in ctx.Categories
orderby ctx.IsNull(c.Description1, "") + ctx.IsNull(c.Description2, "")
select c;
query.Dump();
And will generate T-SQL with ISNULL().
这篇关于LINQ可以到SQL生成包含ISNULL函数TSQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!