vb.net VB linq to sql isnull
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13416280/
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
VB linq to sql isnull
提问by user1253414
I want make this in linq to sql
我想在 linq to sql 中做到这一点
select isnull(x.COD, '1') as 'NCOD'
from table x
I have tried this.
我试过这个。
From x In table
select NCOD = x.COD ?? '1';
I'm using LINQPad 4 and VB Expression. but LINQPAd says that I can't use ? expression (No se puede usar aquí el carácter '?' in Spanish)
我正在使用 LINQPad 4 和 VB 表达式。但是 LINQPAd 说我不能使用?表达(西班牙语中没有 se puede usar aquí el carácter '?')
Any idea?
任何的想法?
PD: sorry for my English.
PD:对不起我的英语。
回答by DarkWanderer
If your value type is string, you can use following expression:
如果您的值类型是字符串,则可以使用以下表达式:
result = from x in values select IF(String.IsNullOrEmpty(x),"No value",x);
If it is a nullable type (like int?, Double?, SomeClass?):
如果它是可空类型(如 int?、Double?、SomeClass?):
result = from x in values select IF(x.HasValue,x,1);
Ref:
参考:
- Nullable types in VB.NET - http://visualbasic.about.com/od/usingvbnet/a/nullabletypes.htm
- Warning about VB IF operator: https://stackoverflow.com/a/11990998/1732138
- VB.NET 中的可空类型 - http://visualbasic.about.com/od/usingvbnet/a/nullabletypes.htm
- 关于 VB IF 运算符的警告:https: //stackoverflow.com/a/11990998/1732138
回答by Sagar Upadhyay
Try the following query, this will be helpful to you.
尝试以下查询,这将对您有所帮助。
var InvoiceNo = dbContext.table.where(x => (int?)x.ICOD) ?? "NCOD";
Here, ??works only on nullable int int?
这里,?? 仅适用于可为空的 int int?
And you are a vb.net user than you should use following code, this will be help ful to you...
而且您是 vb.net 用户,您应该使用以下代码,这将对您有所帮助...
Dim InvoiceNo = If(dbContext.table.where(Function(x) DirectCast(x.ICOD, System.Nullable(Of Integer))), "NCOD")

