所以写作:
if (obj.Collection == null)
obj.Collection = new Collection();
obj.Collection.Add(something);
Run Code Online (Sandbox Code Playgroud)
我想写作:
obj.Collection = obj.Collection ?? new Collection;
obj.Collection.Add(something);
Run Code Online (Sandbox Code Playgroud)
这种感觉有点不对,尤其是这部分"obj.Collection = obj.Collection ......"
你们有什么感想 ?
问候,
我对MSSQL比较陌生,如果这个问题听起来很简单,那就很抱歉.我想用分隔符连接多个字段,.但是,当该字段为空时,额外的内容,也将包含在结果字符串中.那么有一个简单的方法来解决这个问题吗?例如,
SELECT VRI.Street_Number_and_Modifier + ',' +
VRI.Street_Direction + ',' +
VRI.Street_Name + ',' +
VRI.Street_Direction + ',' +
VRI.Street_Suffix + ',' +
VRI.Street_Post_Direction + ',' +
VRI.Unit
FROM View_Report_Information_Tables VRI
Run Code Online (Sandbox Code Playgroud) 我在Postgresql中有这个表:
CREATE TABLE my_table
(
id bigint NOT NULL,
value bigint,
CONSTRAINT my_table_pkey PRIMARY KEY (id)
);
Run Code Online (Sandbox Code Playgroud)
my_table中有大约50000行.
问题是,为什么查询:
SELECT * FROM my_table WHERE id = COALESCE(null, id) and value = ?
Run Code Online (Sandbox Code Playgroud)
比这个慢:
SELECT * FROM my_table WHERE value = ?
Run Code Online (Sandbox Code Playgroud)
除了在app-layer中优化查询字符串之外,还有其他解决方案吗?
编辑:实际上,问题是如何重写查询select * from my_table where id=coalesce(?, id) and value=?以使最坏情况下的性能不低于select * from my_table where value=?Postgresql 9.0中的情况
我想在没有从查询中获取数据时显示默认消息.
例如,让我们进行查询
从员工中选择empname,其中id = 100
如果没有数据匹配此搜索,我希望得到Unavailable结果或显示所需的结果.
那么我应该如何编写SQL查询来实现这一目标.
我正在使用Oracle 10g.
我想将以下查询编写为Hibernate Criteria查询:
select
to_char(nvl(ol.updated_datetime, ol.created_datetime), 'dd/mm/yyyy'), sum(discount_price)
from order_line ol
where nvl(ol.updated_datetime, ol.created_datetime) between to_date('05-may-10') and to_date('30-may-10')
group by to_char(nvl(ol.updated_datetime, ol.created_datetime), 'dd/mm/yyyy')
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将nvl函数转换为Criteria查询等价物.我意识到HQL有一个coalesce表达式,但我想把它写成Criteria查询.
任何建议都会非常感激!
编辑:如果任何人都可以提供执行上述操作的HQL查询,也可以作为我的解决方案.
我有一个脚本,用户可以在多个表中选择一个字段.它使用a Union来获取所有行.现在,我有一个映射数组来指示每个表中是否存在特定字段,如果不存在,则使用它"" as field_name来控制所有内容.
我想知道是否有一种方法可以在查询中引用一个可能不存在的列,类似于COALESCE如果列不存在,而不是抛出错误,它只返回一个默认值.
在SQL中我可以这样做:
Select Coalesce(Property1, Property2, Property3, 'All Null') as Value
From MyTable
Run Code Online (Sandbox Code Playgroud)
如果Property1,2和3都为null,那么我得到'All Null'
我如何在XAML中执行此操作?我试过以下,但没有运气:
<Window.Resources>
<local:Item x:Key="MyData"
Property1="{x:Null}"
Property2="{x:Null}"
Property3="Hello World" />
</Window.Resources>
<TextBlock DataContext="{StaticResource MyData}">
<TextBlock.Text>
<PriorityBinding TargetNullValue="All Null">
<Binding Path="Property1" />
<Binding Path="Property2" />
<Binding Path="Property3" />
</PriorityBinding>
</TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
结果应该是'Hello World',而是'All Null'
我希望我的问题很明确.
在SQL中为了避免获取NULL值,我可以使用"coalesce"函数来替换它,如下所示:
SELECT COALESCE(some_column, 0) FROM some_table;
Run Code Online (Sandbox Code Playgroud)
但我找不到用Sequel做同样事情的方法.
考虑这个愚蠢的域名:
namespace TryHibernate.Example
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
public class WorkItem
{
public int Id { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class Task
{
public int Id { get; set; }
public Employee Assignee { get; set; }
public WorkItem WorkItem { get; set; …Run Code Online (Sandbox Code Playgroud) 如果我有这样的查询:
SELECT COALESCE(
(SELECT value FROM precomputed WHERE ...),
alwaysComputeValue(...)
);
Run Code Online (Sandbox Code Playgroud)
第二个表达式会被评估吗?这还取决于执行计划员还是独立的?
coalesce ×10
sql ×3
c# ×2
postgresql ×2
criteria ×1
data-binding ×1
hibernate ×1
mysql ×1
nhibernate ×1
nvl ×1
oracle ×1
properties ×1
sequel ×1
sql-server ×1
wpf ×1
xaml ×1