OrderBy删除对象属性

时间:2019-03-05 11:37:25

标签: c# entity-framework linq

我有以下代码:

var test = _fitDbContext.MvLatestTestResult
        .Include(r => r.LastTest)
        .Include(r => r.LastTest.Testrun)
        .Include(r => r.LastTest.Testrunconfig)
        .Where(r => r.LastTest.Testrunconfig.Started.Value.Hour >= 0 && r.LastTest.Testrunconfig.Started.Value.Hour < 6)
        .Where(r => r.LastTest.Testrun.Userc == "build")
        .Where(r => r.ProductId == productId)
        .GroupBy(r => r.LastTest.Testrunconfig.Started.Value.Date)
        .OrderByDescending(r => r.Key.Date)
        .Take(3)
        .ToDictionary(group => group.Key, group => group.GroupBy(r => r.Configfilename));

变量test如下所示:

enter image description here

您会看到LastTestnull。如果我删除.OrderByDescending(r => r.Key.Date),则其中包含值。

为什么 OrderByDescending 会删除LastTest值?


编辑

我刚刚发现的有趣的东西。如果我更改GroupByOrderBy的顺序,那么一切都会按预期进行。问题是OrderBy与所有记录一起执行并且花费很长时间。

var test = _fitDbContext.MvLatestTestResult
        .Include(r => r.LastTest)
        .Include(r => r.LastTest.Testrun)
        .Include(r => r.LastTest.Testrunconfig)
        .Where(r => r.LastTest.Testrunconfig.Started.Value.Hour >= 0 && r.LastTest.Testrunconfig.Started.Value.Hour < 6)
        .Where(r => r.LastTest.Testrun.Userc == "build")
        .Where(r => r.ProductId == productId)
        .OrderByDescending(r => r.Key.Date)
        .GroupBy(r => r.LastTest.Testrunconfig.Started.Value.Date)
        .Take(3)
        .ToDictionary(group => group.Key, group => group.GroupBy(r => r.Configfilename));

0 个答案:

没有答案