0

我见过很多人以我个人认为效率不高的方式显示数据记录,我正在遵循这些方法,但并不满意,因为我认为在应用程序生命周期的某个时刻,数据记录可能会达到巨大的数量,这样,在遍历所有记录,生成数据,然后将该数据打印到浏览器时,会减慢应用程序的执行和性能,例如看看这段代码,

var query2 = from m in JE.J_Posted_Jobs_Tbl
join n in JE.J_Genders_Tbl on m.J_Job_Location equals n.J_Gender_ID
select new { 
m.J_Job_ID,
m.J_Job_Title,
m.J_Job_Description,
m.J_Job_Package,
m.J_Job_Timing,
n.J_Gender
};


string datarow = "";  
foreach(var result in query2 ){
datarow = datarow + "<div class='eachjob'>"+
        "<a href='Employeer-Details.aspx'>"+
        "<div class='eachjobCompanyImg'>"+
"<img src='includes/images/premiummembers/premiummeber1.gif' width='60' height='60'/>"+
"</div>"+"</a>"+"<div class='eachjobName'>
<a href='Jobs-Details.aspx?J_PostedJob_ID="+result.J_Job_ID+"'>"+result.J_Job_Title+"
</a>
     </div>"+"<div class='eachjobName'>Islamabad</div>"+
            "<div class='eachjobDescription'>"+
                ""+result.J_Job_Description+""+
        "</div>"+
        "<div class='eachjobName'>"+result.J_Job_Package+"</div>"+
        "<div class='eachjobName'>"+result.J_Job_Timing+"</div>"+
        "<div class='eachjobName'>"+result.J_Gender+"</div>"+
        "<div class='eachjobFuncDiv'>"+
            "<div class='eachjobFunDivBlock'>"+
                "<div class='eachjobFunDivBlockImg'>"+
                    "<img src='includes/images/jobFunLogos/jobMarkHired.png' />"+
                "</div>"+
                "<div class='eachjobFunDivBlockName'>Mark Hired</div>"+
            "</div>"+
             "<div class='eachjobFunDivBlock'>"+
                "<div class='eachjobFunDivBlockImg'>"+
                    "<img src='includes/images/jobFunLogos/jobMakeDisApply.png' />"+
                "</div>"+
                "<div class='eachjobFunDivBlockName'>Disable Job</div>"+
            "</div>"+
        "</div></div>";
}
YourPostedJobs.InnerHtml = datarow;

is this method any efficient? If not, plz tell me an efficient way which is 
application friendly.
Thanks in advance!
4

1 回答 1

2

一个明显的改进是一次返回一页数据,而不是一次返回所有项目。为此,您将使用 LinqSkip()Take()在查询中:

var query2 = from m in JE.J_Posted_Jobs_Tbl
join n in JE.J_Genders_Tbl on m.J_Job_Location equals n.J_Gender_ID

select new { 
m.J_Job_ID,
m.J_Job_Title,
m.J_Job_Description,
m.J_Job_Package,
m.J_Job_Timing,
n.J_Gender
};

var pagedQuery = q2.Skip(pageSize * (page -1)).Take(pageSize);

另一个步骤是通过连接这样的字符串来停止构建您的 HTML。当然,返回数据并让浏览器使用 javascript 渲染它会更好!或者,如果您必须像这样在服务器上构建它,至少使用 aStringBuilderstring.Format().

于 2013-07-08T12:03:09.963 回答