Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq包含在嵌套的分组查询中_C#_.net_Entity Framework_Linq_Entity Framework 6 - Fatal编程技术网

C# Linq包含在嵌套的分组查询中

C# Linq包含在嵌套的分组查询中,c#,.net,entity-framework,linq,entity-framework-6,C#,.net,Entity Framework,Linq,Entity Framework 6,下面我有一个相对复杂的查询,其中包含一些嵌套的GROUPBY查询。问题是,我不知道如何将include添加到任何GROUPBY查询中。有没有办法在EF6的子组查询中包含子属性 return db.PatientOrders .Include(x => x.Patient) // this has no effect .Where(x => !x.ProcessedOn.HasValue && x.Pati

下面我有一个相对复杂的查询,其中包含一些嵌套的GROUPBY查询。问题是,我不知道如何将include添加到任何GROUPBY查询中。有没有办法在EF6的子组查询中包含子属性

return db.PatientOrders
                .Include(x => x.Patient) // this has no effect
                .Where(x => !x.ProcessedOn.HasValue && x.Patient.Home.PharmacyId == pharmacyID)
                .GroupBy(x => x.Patient.Home)
                .ToDictionary(x => x.Key, x => x
                            .ToList()
                            .GroupBy(y => y.Patient.Department)
                            .ToDictionary(y => y.Key, y => y
                                    .Include(x => x.OrderLines) // this does not compile
                                    .ToList()
                                    .GroupBy(z => z.Patient)
                                    .ToDictionary(z => z.Key, z => z.ToList(), new PatientEqualityComparer()), new HomeDepartmentEqualityComparer()), new HomeEqualityComparer());

我找到了一种方法,但我不确定该解决方案是否具有良好的性能

            // group by reshapes query so previous includes are lost
            // solution: flatten after group by then do includes then group by again
            return db.PatientOrders
                .GroupBy(x => x.Patient.Home) // Group
                .SelectMany(g => g.AsEnumerable()) // Ungroup
                .Include(x => x.Patient)
                .Include(x => x.Patient.Home)
                .Include(x => x.Patient.Doctor)
                .Include(x => x.Patient.Department)
                .Include(x => x.OrderLines)
                .Include(x => x.OrderLines.Select(y => y.Product))
                .Where(x => !x.ProcessedOn.HasValue && x.Patient.Home.PharmacyId == pharmacyID)
                .AsEnumerable() // Switch to LINQ to Objects
                .GroupBy(x => x.Patient.Home) // Group again
                .ToDictionary(x => x.Key, x => x
                            .ToList()
                            .GroupBy(y => y.Patient.Department)
                            .ToDictionary(y => y.Key, y => y
                                    .ToList()
                                    .GroupBy(z => z.Patient)
                                    .ToDictionary(z => z.Key, z => z.ToList(), new PatientEqualityComparer()), new HomeDepartmentEqualityComparer()), new HomeEqualityComparer());

我找到了一种方法,但我不确定该解决方案是否具有良好的性能

            // group by reshapes query so previous includes are lost
            // solution: flatten after group by then do includes then group by again
            return db.PatientOrders
                .GroupBy(x => x.Patient.Home) // Group
                .SelectMany(g => g.AsEnumerable()) // Ungroup
                .Include(x => x.Patient)
                .Include(x => x.Patient.Home)
                .Include(x => x.Patient.Doctor)
                .Include(x => x.Patient.Department)
                .Include(x => x.OrderLines)
                .Include(x => x.OrderLines.Select(y => y.Product))
                .Where(x => !x.ProcessedOn.HasValue && x.Patient.Home.PharmacyId == pharmacyID)
                .AsEnumerable() // Switch to LINQ to Objects
                .GroupBy(x => x.Patient.Home) // Group again
                .ToDictionary(x => x.Key, x => x
                            .ToList()
                            .GroupBy(y => y.Patient.Department)
                            .ToDictionary(y => y.Key, y => y
                                    .ToList()
                                    .GroupBy(z => z.Patient)
                                    .ToDictionary(z => z.Key, z => z.ToList(), new PatientEqualityComparer()), new HomeDepartmentEqualityComparer()), new HomeEqualityComparer());