开发者

LINQ MVC 3 and double query in profiler

开发者 https://www.devze.com 2023-04-12 20:21 出处:网络
I\'ve installed Linq To Sql Profiler and notice, th开发者_运维技巧at i have double query executing (or i\'m wrong). So:

I've installed Linq To Sql Profiler and notice, th开发者_运维技巧at i have double query executing (or i'm wrong). So:

    int categoriesCount = _dataManager.PeopleTalesCategories.GetPeopleTalesCategoriesCount();

Here i have first query:

SELECT COUNT(*) AS [value]
FROM   [dbo].[PeopleTalesCategories] AS [t0]

Then:

    IEnumerable<PeopleTalesCategory> categories = _dataManager.PeopleTalesCategories.GetAllCategories((int)ViewData["CurrentPage"] * CategoriesOnPage, CategoriesOnPage);
    ViewBag.Categories = categories;
    return View();

Here i have second query:

SELECT ...
FROM   (SELECT ROW_NUMBER() OVER (ORDER BY [t0].[PositionInMenu], [t0].[NameAn]) AS [ROW_NUMBER],
               ...
        FROM   [dbo].[PeopleTalesCategories] AS [t0]) AS [t1]
WHERE  [t1].[ROW_NUMBER] BETWEEN 0 /* @p0 */ + 1 AND 0 /* @p0 */ + 15 /* @p1 */
ORDER  BY [t1].[ROW_NUMBER]

And the most interesting, when i starting to display my categories into HTML markup here:

@foreach (var c in ViewBag.Categories)
{
    <h3><a href="/narodnie-skazki/@c.RouteNameAn">@c.NameAn</a> <span>(@c.tCountInCategory)</span></h3>
    <p>@c.Description</p>
}

i get the third query, like second.

So, does anybody know why i have 3 query in Linq To Sql Profiler? Is it normal?

Thx alot.

Link to profiler: http://l2sprof.com/


I'm guessing that GetAllCategories returns an IQueryable which gets enumerated twice, although I'm not sure why that would occur here. At any rate, it might help if you use .ToList() when returning it in GetAllCategories or when assigning it to the viewbag:

ViewBag.Categories = categories.ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号