Linq to sql 中字符串为null时该怎么写?

powerxsydj 2011-03-17 06:32:29
sql server 2005中有一张PartInfo表中有以下几个字段parentCode, GoodsCode, 其中parentCode是允许为null的,如根节点就会如此,写了一个方法用于获取某一条记录信息
PartInfo GetData(string parentCode, string goodsCode)
{
return (from r in DataContext.PartInfo
where r.ParentCode == parentCode && r.GoodsCode == goodsCode
select r).Single();
}

参数parentCode不为null时都是有效的,为null时linq生成的代码如下:
SELECT [t0].[ID], [t0].[ProductCode], [t0].[ParentCode], [t0].[GoodsCode]
FROM [dbo].[PartInfo] AS [t0]
WHERE (([t0].[ParentCode] = '') AND ([t0].[GoodsCode] = 'TEST 110300001')

我希望能够生成为:
SELECT [t0].[ID], [t0].[ProductCode], [t0].[ParentCode], [t0].[GoodsCode]
FROM [dbo].[PartInfo] AS [t0]
WHERE (([t0].[ParentCode] is null ) AND ([t0].[GoodsCode] = 'TEST 110300001')

请问Linq的代码该怎么写?
...全文
329 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2011-03-18
  • 打赏
  • 举报
回复
 return (from r in DataContext.PartInfo  
where r.GoodsCode == goodsCode &&
r.ParentCode ==(string.IsNullOrEmpty(parentCode)?null: parentCode)
select r).Single();
powerxsydj 2011-03-18
  • 打赏
  • 举报
回复
分不够再加啊
powerxsydj 2011-03-18
  • 打赏
  • 举报
回复
先感谢各位高手的回贴,1楼的肯定是可以用的,我后来就是用类似的方式实现的,我想知道的是Linq有没有string类型在where语句中自动生成sql 的is null 或 not is null 的代码的方式,而不用写一堆的if,else。
claymore1114 2011-03-18
  • 打赏
  • 举报
回复
NUll 和 "" 不是一个概念,感觉 也不用特殊处理了。
如果程序中 要把"" 变成了 NULL, 那数据库中的 空值不是Null的怎么处理,都匹配不到。
powerxsydj 2011-03-18
  • 打赏
  • 举报
回复
太感谢了,给分,要多给分该怎么做呢?
powerxsydj 2011-03-18
  • 打赏
  • 举报
回复
感谢楼主回复,以上写法与我的写法生成的SQL语句完全一致,当parentCode参数为null时与楼主的写法是没有差异的。
var result =
from r in DataContext.PartInfo
where r.GoodsCode == goodsCode &&
r.ParentCode == null select r;
生成的SQL语句就是:
SELECT [t0].[ID], [t0].[ProductCode], [t0].[ParentCode], [t0].[GoodsCode]
FROM [dbo].[PartInfo] AS [t0]
WHERE (([t0].[ParentCode] = '') AND ([t0].[GoodsCode] = 'TEST 110300001')

而不是(([t0].[ParentCode] is null))
是不是LINQ不能生成这种代码啊?
q107770540 2011-03-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 claymore1114 的回复:]

不用判断 , 可空类型用 Nullable<T>.Equals,引用类型用Nullable.Equals。
如下:
C# code

return (from r in DataContext.PartInfo
where Nullable.Equals(r.ParentCode, parentCode) &&
r.GoodsCode……
[/Quote]
这种方法是可以的 针对 null情况是可以翻译成is null的,但是对于" "空格则不行
要做一点改动:(以nortwind为例:)

var query=from c in this.Customers
where Nullable.Equals(c.Region,s==null?null:string.IsNullOrEmpty(s.Trim())?null:s)
select c;

翻译成的SQL:

SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
FROM [Customers] AS [t0]
WHERE [t0].[Region] IS NULL

claymore1114 2011-03-18
  • 打赏
  • 举报
回复
不用判断 , 可空类型用 Nullable<T>.Equals,引用类型用Nullable.Equals。
如下:

return (from r in DataContext.PartInfo
where Nullable.Equals(r.ParentCode, parentCode) &&
r.GoodsCode == goodsCode select r).Single();
claymore1114 2011-03-17
  • 打赏
  • 举报
回复

turn (from r in DataContext.PartInfo
where r.ParentCode.Equals(parentCode) &&
r.GoodsCode == goodsCode select r).Single();

claymore1114 2011-03-17
  • 打赏
  • 举报
回复

return (from r in DataContext.PartInfo
where Nullable<decimal>.Equals(r.ParentCode, parentCode) &&
r.GoodsCode == goodsCode select r).Single();
q107770540 2011-03-17
  • 打赏
  • 举报
回复
PartInfo GetData(string parentCode, string goodsCode)
{
var query=from r in DataContext.PartInfo
where r.GoodsCode == goodsCode
select r;
if(string.IsNullOrEmpty(parentCode))
{
query=query.Where(r=>r.ParentCode == null);
}
else
{
query=query.Where(r=>r.ParentCode == parentCode);
}
return query.SingleOrDefault();
}









论坛签名======================================================================

powerxsydj:你好!
截至 2011-03-17 18:32:28 前:
你已发帖 12 个, 未结贴 1 个;
结贴率为: 91.67%

当您的问题得到解答后请及时结贴.

http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html

如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖

如何给自己的回帖中也加上签名?
http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧