Majid Khan Mohmand
Majid Khan Mohmand

Reputation: 93

How to show multiple records in asp.net using EF/LINQ(The best approach)

I've seen a lot of people showing data records in a way which i think personally is not efficient, i am following those methods but am not satisfied as i think at some point in the application life time, data records might reach an enormous amount, and that way, while looping through all the records, generating data, and then printing that data to the browser will slow down the application execution and performance, as an example take a look at this code,

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!

Upvotes: 0

Views: 115

Answers (1)

Mike Goodwin
Mike Goodwin

Reputation: 8880

One obvious improvement would be to return the data a page at a time rather than every item in one go. To do this, you would use Linq Skip() and Take() in your query:

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);

Another step would be to stop building your HTML by concatenating strings like that. Surely it would be better to return the data and have the browser render it using javascript! Or if you must build it on the server like that, at least either use a StringBuilder or string.Format().

Upvotes: 2

Related Questions