5

我有以下代码:

    [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
        };

d.Createdd.CreatedBy和有可能为空 d.Modifiedd.ModifiedBy

我怎样才能做到这一点,如果它们为空,那么 select 返回n/aandCreatedBy并返回and的ModifiedBy日期?January, 1, 2012CreatedModified

4

1 回答 1

10

您可以使用空合并运算符( ??) 为可空值类型或引用类型提供默认值:

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"
};
于 2012-08-19T06:17:32.217 回答