C# 等价于 LINQ 中的 SQL ISNULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/413084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of SQL ISNULL in LINQ?
提问by MartGriff
In SQL you can run a ISNULL(null,'') how would you do this in a linq query?
在 SQL 中,您可以运行 ISNULL(null,'') 在 linq 查询中您将如何执行此操作?
I have a join in this query:
我加入了这个查询:
var hht = from x in db.HandheldAssets
join a in db.HandheldDevInfos on x.AssetID equals a.DevName into DevInfo
from aa in DevInfo.DefaultIfEmpty()
select new
{
AssetID = x.AssetID,
Status = xx.Online
};
but I have a column that has a bit type that is non nullable (xx.online) how can I set this to false if it is null?
但我有一列的位类型不可为空 (xx.online) 如果它为空,我该如何将其设置为 false?
采纳答案by Marc Gravell
Since aa
is the set/object that might be null, can you check aa == null
?
由于aa
集合/对象可能为空,您能检查一下aa == null
吗?
(aa
/ xx
might be interchangeable (a typo in the question); the original question talks about xx
but only defines aa
)
(aa
/xx
可能可以互换(问题中的错字);原始问题讨论xx
但仅定义aa
)
i.e.
IE
select new {
AssetID = x.AssetID,
Status = aa == null ? (bool?)null : aa.Online; // a Nullable<bool>
}
or if you want the default to be false
(not null
):
或者,如果您希望默认值为false
(not null
):
select new {
AssetID = x.AssetID,
Status = aa == null ? false : aa.Online;
}
Update; in response to the downvote, I've investigated more... the fact is, this is the right approach! Here's an example on Northwind:
更新; 为了回应downvote,我进行了更多调查......事实是,这是正确的方法!以下是 Northwind 的示例:
using(var ctx = new DataClasses1DataContext())
{
ctx.Log = Console.Out;
var qry = from boss in ctx.Employees
join grunt in ctx.Employees
on boss.EmployeeID equals grunt.ReportsTo into tree
from tmp in tree.DefaultIfEmpty()
select new
{
ID = boss.EmployeeID,
Name = tmp == null ? "" : tmp.FirstName
};
foreach(var row in qry)
{
Console.WriteLine("{0}: {1}", row.ID, row.Name);
}
}
And here's the TSQL - pretty much what we want (it isn't ISNULL
, but it is close enough):
这是 TSQL - 几乎是我们想要的(不是ISNULL
,但已经足够接近了):
SELECT [t0].[EmployeeID] AS [ID],
(CASE
WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(10),@p0)
ELSE [t2].[FirstName]
END) AS [Name]
FROM [dbo].[Employees] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[FirstName], [t1].[ReportsTo]
FROM [dbo].[Employees] AS [t1]
) AS [t2] ON ([t0].[EmployeeID]) = [t2].[ReportsTo]
-- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
QED?
QED?
回答by Ray Booysen
Looks like the type is boolean and therefore can never be null and should be false by default.
看起来类型是布尔值,因此永远不能为空,默认情况下应该为假。
回答by bruno conde
You can use the ??
operator to set the default value but first you must set the Nullable
property to true
in your dbmlfile in the required field (xx.Online
)
您可以使用??
运营商设置的默认值,但首先你必须设置Nullable
属性为true
您的dbml在必填字段文件(xx.Online
)
var hht = from x in db.HandheldAssets
join a in db.HandheldDevInfos on x.AssetID equals a.DevName into DevInfo
from aa in DevInfo.DefaultIfEmpty()
select new
{
AssetID = x.AssetID,
Status = xx.Online ?? false
};
回答by Slaggg
I often have this problem with sequences (as opposed to discrete values). If I have a sequence of ints, and I want to SUM them, when the list is empty I'll receive the error "InvalidOperationException: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.".
我经常遇到序列问题(与离散值相反)。如果我有一个整数序列,并且我想对它们求和,当列表为空时,我将收到错误“InvalidOperationException:无法将空值分配给 System.Int32 类型的成员,这是一个不可为空的值类型。”。
I find I can solve this by casting the sequence to a nullable type. SUM and the other aggregate operators don't throw this error if a sequence of nullable types is empty.
我发现我可以通过将序列转换为可空类型来解决这个问题。如果可空类型序列为空,则 SUM 和其他聚合运算符不会引发此错误。
So for example something like this
所以例如这样的事情
MySum = MyTable.Where(x => x.SomeCondtion).Sum(x => x.AnIntegerValue);
becomes
变成
MySum = MyTable.Where(x => x.SomeCondtion).Sum(x => (int?) x.AnIntegerValue);
The second one will return 0 when no rows match the where clause. (the first one throws an exception when no rows match).
当没有行匹配 where 子句时,第二个将返回 0。(第一个在没有行匹配时抛出异常)。