LINQ:显示空列表的结果

Dou*_* M. 6 c# linq

我在C#中创建了两个实体(简化):

class Log {  
    entries = new List<Entry>();

    DateTime Date { get; set; }  
    IList<Entry> entries { get; set; }  
}  

class Entry {  
    DateTime ClockIn { get; set; }  
    DateTime ClockOut { get; set; }  
}  
Run Code Online (Sandbox Code Playgroud)

我使用以下代码初始化对象:

Log log1 = new Log() {
    Date = new DateTime(2010, 1, 1),                
};
log1.Entries.Add(new Entry() {
    ClockIn = new DateTime(0001, 1, 1, 9, 0, 0),
    ClockOut = new DateTime(0001, 1, 1, 12, 0, 0)
});

Log log2 = new Log()
{
    Date = new DateTime(2010, 2, 1),
};
Run Code Online (Sandbox Code Playgroud)

以下方法用于获取日期日志:

var query =  
    from l in DB.GetLogs()
    from e in l.Entries  
    orderby l.Date ascending  
    select new  
    {  
        Date = l.Date,  
        ClockIn = e.ClockIn,  
        ClockOut = e.ClockOut,  
    };  
Run Code Online (Sandbox Code Playgroud)

上面的LINQ查询的结果是:

/*
 Date        | Clock In   | Clock Out
 01/01/2010  | 09:00      | 12:00    
*/
Run Code Online (Sandbox Code Playgroud)

我的问题是,重写上面的LINQ查询以包含我创建的第二个对象(Log2)的结果的最佳方法是什么,因为它有一个空列表.换句话说,我想显示所有日期,即使它们没有时间值.

预期结果将是:

/*
 Date        | Clock In   | Clock Out
 01/01/2010  | 09:00      | 12:00    
 02/01/2010  |            |            
*/
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 10

试试这个:

var query =  
    from l in DB.GetLogs()
    from e in l.Entries.DefaultIfEmpty()
    orderby l.Date ascending  
    select new  
    {  
        Date = l.Date,  
        ClockIn = e == null ? null : e.ClockIn,  
        ClockOut = e == null ? null : e.ClockOut,  
    };
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅文档DefaultIfEmpty.

编辑:您可能只想更改它以执行内存中的最后部分:

var dbQuery =  
    from l in DB.GetLogs()
    from e in l.Entries.DefaultIfEmpty()
    orderby l.Date ascending  
    select new { Date = l.Date, Entry = e };

var query = dbQuery.AsEnumerable()
                   .Select(x => new { 
                        Date = x.Date,
                        ClockIn = x.Entry == null ? null : x.Entry.CLockIn,
                        ClockOut = x.Entry == null ? null : x.Entry.CLockOut
                    });
Run Code Online (Sandbox Code Playgroud)