[英]LINQ: display results from empty lists
我在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; }
}
我使用以下代码初始化对象:
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),
};
以下方法用于获取日期日志:
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,
};
上面的LINQ查询的结果是:
/*
Date | Clock In | Clock Out
01/01/2010 | 09:00 | 12:00
*/
我的问题是,重写上面的LINQ查询以包含我创建的第二个对象(Log2)的结果的最佳方法是什么,因为它有一个空列表。 换句话说,我想显示所有日期,即使它们没有时间值。
预期结果将是:
/*
Date | Clock In | Clock Out
01/01/2010 | 09:00 | 12:00
02/01/2010 | |
*/
尝试这个:
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,
};
有关它的更多信息,请参阅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
});
这是建立在Jon的解决方案之上。 使用它我收到以下错误:
无法分配给匿名类型属性
我已将Jon的示例更新为以下内容,它似乎给出了所需的结果:
var logs = new []{log1,log2};
var query =
from l in logs.DefaultIfEmpty()
from e in l.entries.DefaultIfEmpty()
orderby l.Date ascending
select new
{
Date = l.Date,
ClockIn = e == null ? (DateTime?)null : e.ClockIn,
ClockOut = e == null ? (DateTime?)null : e.ClockOut,
};
query.Dump();
安德鲁
PS .Dump()是由于我使用LINQ Pad。
您应该看一下LINQ Union Operator。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.