获取导航属性的Count()而不在Entity Framework中加载整个集合

时间:2020-12-17 20:13:31

In XAML DataGrid is bound to a list of EF entities called Results. One of columns is bound to Count of Buildings navigation property. Lazy loading is off. So I need to include Buildings in query, to get it's count. This causes a performance issues because whole Buildings entity collection loads in memory. But I just need a Count of it. Is there any way to get Count of navigation property without loading it in memory?

在XAML中,DataGrid绑定到名为Results的EF实体列表。其中一列绑定到“建筑物数量”导航属性。延迟加载已关闭。所以我需要在查询中包含建筑物,以便计算它。这会导致性能问题,因为整个Buildings实体集合会在内存中加载。但我只需要一个计数。有没有办法获得导航属性的计数而不将其加载到内存中?

  var resQuery =
                db.BAStreets
                    .Include("Street.StreetType")
                    .Include("Area.District")
                    .Include("Buildings")
                    .Where(x => true);
  Results = resQuery.ToList();

Binding in XAML:

在XAML中绑定:

<DataGridTextColumn Binding="{Binding Buildings.Count}"/>

And little additional question. I use this: .Where(x => true) to cast DbSet to IQueryable. Looks like this is a smell thing. What is standard pattern?

还有一点其他问题。我使用:.Where(x => true)将DbSet强制转换为IQueryable。看起来这是一种气味的东西。什么是标准模式?

1 个解决方案

#1


If you just need the Count then you could create following Query

如果您只需要Count,那么您可以创建以下Query

Results = db.BAStreets.Select(i => new
                                {
                                    Street = i,
                                    StreetType = i.Street.StreetType,
                                    District = i.Area.District,
                                    BuildingCount = i.Buildings.Count()
                                }).ToList()

Then in XAML you bind to the BuildingCount property instead of the other.

然后在XAML中绑定到BuildingCount属性而不是另一个属性。

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Street}"/>
        <DataGridTextColumn Binding="{Binding StreetType }"/>
        <DataGridTextColumn Binding="{Binding District }"/>
        <DataGridTextColumn Binding="{Binding BuildingCount }"/>
    </DataGrid.Columns>
</DataGrid>

#1


If you just need the Count then you could create following Query

如果您只需要Count,那么您可以创建以下Query

Results = db.BAStreets.Select(i => new
                                {
                                    Street = i,
                                    StreetType = i.Street.StreetType,
                                    District = i.Area.District,
                                    BuildingCount = i.Buildings.Count()
                                }).ToList()

Then in XAML you bind to the BuildingCount property instead of the other.

然后在XAML中绑定到BuildingCount属性而不是另一个属性。

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Street}"/>
        <DataGridTextColumn Binding="{Binding StreetType }"/>
        <DataGridTextColumn Binding="{Binding District }"/>
        <DataGridTextColumn Binding="{Binding BuildingCount }"/>
    </DataGrid.Columns>
</DataGrid>