减少往返次数 - linq-to-sql
对于以下特定情况和一般情况,是否可以减少在执行 ASP.NET MVC 2 操作期间到数据库的往返次数? 我正在使用 linq-to-sql。 以下代码会产生类似 60 个选择的结果,这需要 60 次往返数据库。如何减少往返次数?
如果需要更多我的代码,我会发布它。
我的视图模型:
public class ArticlesContainerViewModel
{
public ArticlesContainerViewModel()
{
}
public Article CategoryArticle { get; set; }
public IList<ArticlesNode> CategoryNodes { get; set; }
}
public class ArticlesNode
{
public Article NodeArticle { get; set; }
public IQueryable<Article> NodeItems { get; set; }
}
视图:
<ul class="tabs">
<% foreach (var category in Model)
{ %>
<li><a href="#" class="s">
<%= Html.Encode(category.CategoryArticle.AbbreviatedTitle) %></a></li>
<% } %>
</ul>
<!-- tab "panes" -->
<div class="panes">
<% foreach (var category in Model)
{
%>
<div>
<table style="width: 100%;" cellpadding="0" cellspacing="0">
<% int counter = 0; foreach (var node in category.CategoryNodes)
{
%>
<%if (counter % 2 == 0)
{ %>
<tr>
<%} %>
<td class="content-container">
<div class="index-node-title">
<%= Html.Encode(node.NodeArticle.ArticleTitle)%> <span class="small1 darkGrey2">(<%= Html.Encode(node.NodeItems.Count().ToString())%>)</span>
</div>
<div class="index-node-content">
<ul class="index-node-content-list">
<% foreach (var item in node.NodeItems.Take(2))
Is it possible to reduce the number of round trips to the database during the execution of an asp.net mvc 2 action for the following specific case and in general?
I'm using linq-to-sql.
The following code results in something like 60 selects, which take 60 round trips to the database. How can I reduce the number of round trips ?
If more of my code is needed, I will post it.
My view model :
public class ArticlesContainerViewModel
{
public ArticlesContainerViewModel()
{
}
public Article CategoryArticle { get; set; }
public IList<ArticlesNode> CategoryNodes { get; set; }
}
public class ArticlesNode
{
public Article NodeArticle { get; set; }
public IQueryable<Article> NodeItems { get; set; }
}
The view:
<ul class="tabs">
<% foreach (var category in Model)
{ %>
<li><a href="#" class="s">
<%= Html.Encode(category.CategoryArticle.AbbreviatedTitle) %></a></li>
<% } %>
</ul>
<!-- tab "panes" -->
<div class="panes">
<% foreach (var category in Model)
{
%>
<div>
<table style="width: 100%;" cellpadding="0" cellspacing="0">
<% int counter = 0; foreach (var node in category.CategoryNodes)
{
%>
<%if (counter % 2 == 0)
{ %>
<tr>
<%} %>
<td class="content-container">
<div class="index-node-title">
<%= Html.Encode(node.NodeArticle.ArticleTitle)%> <span class="small1 darkGrey2">(<%= Html.Encode(node.NodeItems.Count().ToString())%>)</span>
</div>
<div class="index-node-content">
<ul class="index-node-content-list">
<% foreach (var item in node.NodeItems.Take(2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你问我,代码有点混乱,我猜
Model
的类型是IList
类型或类似的类型。您的问题是您在应用程序中使用 IQueryable 这么“高”。我想要的结构是:这样您应该只能获得两个查询,而不是像现在这样的
m*(n+1)
。The code is a little bit messy if you ask me, and I guess that the type of
Model
is of typeIList<ArticlesContainerViewModel>
or something similar. Your problem is that you are usingIQueryable
this "high" up in the application. The structure I would go for is:That way you should be able to get only two queries instead of
m*(n+1)
as you are having now.看看 PLINQO。它将优化您的查询。 www.plinqo.com
Take a look at PLINQO. It will optimize your queries. www.plinqo.com