我有以下代码:
[DisplayName("Created")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? Created { get; set; }
[DisplayName("Modified By")]
public string ModifiedBy { get; set; }
[DisplayName("Modified")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? Modified { get; set; }
from d in data
select new Content.Grid
{
PartitionKey = d.PartitionKey,
RowKey = d.RowKey,
Order = d.Order,
Title = d.Title,e
Created = d.Created,
CreatedBy = d.CreatedBy,
Modified = d.Modified,
ModifiedBy = d.ModifiedBy
};
Run Code Online (Sandbox Code Playgroud)
还有一个可能性d.Created,d.CreatedBy,d.Modified并且d.ModifiedBy可以为null.
我怎样才能让这个如果他们是null,则选择回到n/a了CreatedBy和ModifiedBy返回日期January, 1, 2012为Created和Modified?
nem*_*esv 10
您可以使用null-coalescing operator(??)为可为空的值类型或引用类型提供默认值:
from d in data
select new Content.Grid
{
PartitionKey = d.PartitionKey,
RowKey = d.RowKey,
Order = d.Order,
Title = d.Title,e
Created = d.Created ?? new DateTime(2012,1,1),
CreatedBy = d.CreatedBy ?? "n/a",
Modified = d.Modified ?? new DateTime(2012,1,1),
ModifiedBy = d.ModifiedBy ?? "n/a"
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4947 次 |
| 最近记录: |