简体   繁体   English

使用LINQ和Entity Framework获取延迟加载的子级集合的计数

[英]Get Count of Lazy Loaded Child Collection Using LINQ and Entity Framework

I have a class such as: 我有一个像这样的课程:

public class Category
{
    public int CategoryId { get; set; }
    [Required]
    public string Name { get; set; }
    [NotMapped]
    public int ProductCount { get; set; }
    public virtual IEnumerable<Product> Products { get; set; }
}

I have a query that currently looks like this: 我有一个查询,当前看起来像这样:

context.Categories.SortBy(sortByExpression).Skip(startRowIndex).Take(maximumRows)

I need to include the ProductCount field in my category grid. 我需要在类别网格中包含ProductCount字段。 Is it possible to extend this LINQ query to give me the ProductCount property without reading in the entire Product collection? 是否可以扩展此LINQ查询以提供给我ProductCount属性,而无需阅读整个Product集合? I can do this in a sub query using good old SQL, but I am stumped using LINQ. 我可以在使用良好的旧SQL的子查询中执行此操作,但是我却无法使用LINQ。

Thanks 谢谢

Is this roughly what you are looking for: 这大致就是您要寻找的内容:

context.Categories.SortBy(sortByExpression).Skip(startRowIndex).Take(maximumRows)
    .Select(c => new { c.CategoryId, c.Name, ProductCount = c.Products.Count() });

or have I misunderstood your question? 还是我误解了你的问题?

updated the code 更新了代码

Your question is not clear as enough but if I understood well you want the new column in result for example productCount and you did it with SQL query like below: 您的问题还不够清楚,但是如果我理解得很好,那么您想在结果中添加新列,例如productCount,并使用如下所示的SQL查询来完成:

SELECT c.*, , (SELECT COUNT(*) FROM Products p WHERE p.categoryId = c.id) AS productCount FROM
Categories c
WHERE <Conditions>

Now you want to be able do that using linq(If your question really this), you can do it as below: 现在,您希望能够使用linq来做到这一点(如果您的问题确实如此),则可以按照以下步骤进行操作:

List<Category> categoryList = (from c in context.Categories let pCount = c.Products.Count() select
new { categoryId = c.CategoryId, CategoryName = c.Name, productCount = pCount})
.OrderBy([sortByExpression]).Skip([startRowIndex]).Take([maximumRows]).ToList();

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架延迟加载集合有时为null - Entity Framework lazy loaded collection sometimes null 实体框架中按属性求和的相关集合的总和(延迟加载) - Sum Related Collection By Property in Entity Framework (Lazy Loaded) 在linq Entity Framework中计算子匹配 - Count child matches in linq Entity Framework 如何使用实体框架对延迟加载的实体进行排序? - How to sort lazy-loaded entities, using entity framework? 实体框架5:实体是延迟加载的,但是在观察Local集合时导航属性为null - Entity Framework 5: Entity is lazy loaded but the navigation properties are null when observing the Local collection 带有子集合的 Select 的动态 LINQ 表达式(实体框架) - Dynamic LINQ expression for Select with child collection (Entity Framework) 实体框架 LINQ 等效于 TSQL 以包括子记录计数 - Entity Framework LINQ equivalent of TSQL to include count of child records 实体框架核心从父子集合中获取单个子 - Entity framework core get single child from of parent child collection 使用EntityFramework更新实体,包括延迟加载的子项 - Updating entity including lazy loaded child with EntityFramework 实体框架-导航属性是否不渴望/延迟加载? - Entity Framework - Navigation Property Not Eager/Lazy Loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM