我的查询如下所示:
var products = from p in Products
select new
{
ProductId = p.ProductId,
Description = p.Quantity + " x " p.Price + ", " + p.ItemDescription
};
Run Code Online (Sandbox Code Playgroud)
我在查询中加入描述的原因是,我正在为多个查询/对象执行此操作,以创建历史屏幕(有点像审计屏幕).屏幕需要一段时间才能加载,因此我正在接受所有查询并执行操作
products.Concat(otherProducts);
Run Code Online (Sandbox Code Playgroud)
速度已大大增加(2-3分钟缩短到2-3秒),但是,如果在示例中,p.ItemDescription(数据库中的VARCHAR(50))为空,但数量和价格不为空,然后整个Description字段变为null.
有没有人遇到过这个怪癖?任何人都知道如何让它显示"4 x 5.99",而不是仅仅将其设置为null?
任何帮助将不胜感激,我一直试图解决这个问题,甚至不知道如何在谷歌上搜索这个.
这不是LINQ-to-SQL"quirk",这是标准的SQL行为.
如果我有这样的表达:
columnA + columnB + columnC
Run Code Online (Sandbox Code Playgroud)
而这些列中的任何一个都是null,那么整个表达式将被评估为null.
您希望将空值合并为空字符串.试试这个:
select new
{
ProductId = p.ProductId,
Description = p.Quantity + " x " +
p.Price + ", " +
(p.ItemDescription ?? "")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
899 次 |
| 最近记录: |