我正在研究下面的Linq查询,我无法开始工作。我的问题是我得到一个空引用或没有结果(如果我在for循环中保留“if”语句)。该查询应该通过event_number提供前20个结果时间。
var queryByEventNumber = EventNumbers
.GroupJoin(EntryEvents, e => e.Id, ee => ee.EventNumberId, (e, g) => new
{
EventNumber = e,
EntryEvents = g
})
.OrderBy(e => e.EventNumber.Event_Number);
foreach (var group in queryByEventNumber)
{
Console.WriteLine($"EventNumber: { group.EventNumber.Event_Number}");
//If statement apparently always returns false at this point
//If I remove if statement, I get a null reference on group.EntryEvents.OrderBy(r => r.Result.AdjustTime)...
if(Results.Any(r => r.EntryEvents.Any(g => g.EventNumberId == group.EventNumber.Id)))
{
foreach (var result in group.EntryEvents.OrderBy(r => r.Result.AdjustedTime).Take(20))
{
Console.WriteLine($"\t\t{result.Result.AdjustedTime} : {result.Result.DateCreated.Year}");
}
}
}
问题是我没有每个eventnumber的结果(最终我会,但我相信我需要能够以任何方式处理它)。我已经尝试了if语句的一些变化,但没有运气。任何帮助表示赞赏。谢谢!
答案 0 :(得分:0)
好的,经过一系列的反复试验后,我选择了这个解决方案。不确定它是否是最佳的,但它有效...
var eventNumbers = EventNumbers.Where(e => e.Active).OrderBy(e => e.Event_Number).ToList();
foreach (var en in EventNumbers)
{
Console.WriteLine($"EventNumber: {en.Event_Number}");
var results = Results
.Where(r => r.EntryEvent.EventNumberId == en.Id && !r.Disqualified && r.Active && !r.EntryEvent.Scratched)
.OrderBy(r => r.AdjustedTime)
.Take(20);
if (results != null)
{
foreach (var result in results)
{
Console.WriteLine($"Time: {result.AdjustedTime}");
}
}
}