有什么办法可以提高效率吗?它运行速度快,只是希望改进(如果有的话)

发布于 2024-10-20 15:51:14 字数 905 浏览 0 评论 0原文

有什么办法可以提高效率吗?

internal Func<enntities, IQueryable<CategoryList>> GetCategoryListWithPostingCount =
        CompiledQuery.Compile((entities entities) => 
            from c in entities.Categories.Include("Postings_Categories")
            where c.ParentCategoryID == null
            orderby c.DisplayOrder
            select new CategoryList
            {
                ParentCategoryName = c.CategoryName,
                ParentCategoryID = c.CategoryID,
                SubCategories = (
                from s in entities.Categories
                where s.ParentCategoryID == c.CategoryID
                select new SubCategoryList
                {
                    PostingCount = s.Postings_Categories.Count,
                    SubCategoryName = s.CategoryName,
                    SubCategoryID = s.CategoryID
                })
            });

Any way to make this more efficient?

internal Func<enntities, IQueryable<CategoryList>> GetCategoryListWithPostingCount =
        CompiledQuery.Compile((entities entities) => 
            from c in entities.Categories.Include("Postings_Categories")
            where c.ParentCategoryID == null
            orderby c.DisplayOrder
            select new CategoryList
            {
                ParentCategoryName = c.CategoryName,
                ParentCategoryID = c.CategoryID,
                SubCategories = (
                from s in entities.Categories
                where s.ParentCategoryID == c.CategoryID
                select new SubCategoryList
                {
                    PostingCount = s.Postings_Categories.Count,
                    SubCategoryName = s.CategoryName,
                    SubCategoryID = s.CategoryID
                })
            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-10-27 15:51:14

任何改进建议都取决于我们了解的不仅仅是 LINQ 查询。例如,如果每个类别有很多子类别列表,并且类别附加有很多标量数据(大描述文本等),则在单独的数据库往返中提取类别列表可能会更快。但可能不会。

查看连接了哪些表并添加适当的索引也可能会有所不同。

但总而言之,我想说这是一个过早优化的情况。代码看起来很干净,我可以告诉你你想用它做什么,所以现在就说这很好。如果您发现这是程序中的一个慢点,请担心它。

PS--这个问题被标记为 LINQ-to-SQL,但这对我来说看起来像 LINQ-to-Entities...

Any suggestions for improvement would depend on us knowing a lot more than just the LINQ query. For example, if you have a lot of SubCategoryLists per Category, and if Category has a lot of scalar data attached to it (big description text, etc), it might be faster to pull the category list in a separate database round-trip. But it probably won't.

Looking at what tables are joined and adding the appropriate indices could make a difference, too.

But all in all, I'd say this is a case of premature optimization. The code looks clean, and I can tell what you're trying to do with it, so call that good for now. If you find that it's a slow point in your program, worry about it then.

PS--The question is tagged LINQ-to-SQL, but this looks like LINQ-to-Entities to me...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文