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

、、、がnullになる 可能性がd.Createdあります。d.CreatedByd.Modifiedd.ModifiedBy

それらがnullの場合、selectがandを返し、andの日付を返すようにするにはどうすれn/aばよいですか?CreatedByModifiedByJanuary, 1, 2012CreatedModified

4

1 に答える 1

10

null合体演算子( )を使用して、 null??許容値型または参照型のデフォルト値を指定できます。

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 に答える