locked
For each RRS feed

  • Question

  • User1519602350 posted

    foreach(var row in rowsthree){} affects the performance of loading time, instead of foreach() what can i use?

    Thanks

    Wednesday, May 8, 2013 1:16 PM

Answers

  • User281315223 posted

    I'm not sure why you would be continually encountering performance issues based on the type of loop that you are using. The performance of foreach and a for loop should be relatively close, which leads me to believe that the issue could actually be related to your database query that may be slowing things down.

    Are you recieving any intellisense when you are using the following code : 

    for(int i = 0; i < rows.Count(); i++){
          //Check the following for intellisense
          var row = rows[i];
    }

    As the Database.Query() method that you are using returns an IEnumerable<Object> so you should be able to access the values accordingly. You may want to try not using var and instead using an object instead to see if that makes any difference and you should be able to refer to your explicit columns as well :

    for(int i = 0; i < rows.Count(); i++){
    //Example of using an object
    object row = rows[i];
    //Example of explicitly trying to access a row property var property = rows[i]["Your Column Name"]; }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2013 8:00 AM

All replies

  • User-1137493631 posted

    you could use for loop or while loop, foreach will be used to itterate some collections which use IEnum objects.

    Wednesday, May 8, 2013 1:20 PM
  • User1519602350 posted

    i am using foreach() for to receive collection of data from the database. Is it possbile to use for loop for this?

    Wednesday, May 8, 2013 1:24 PM
  • User-821857111 posted

    foreach(var row in rowsthree){} affects the performance of loading time

    How many rows do you return? Perhaps you should limit the number of rows by using Offest and Fetch in SQL CE or Row_Number in SQL Server. 

    Wednesday, May 8, 2013 1:43 PM
  • User281315223 posted

    Typically the performance of a foreach loop shouldn't be extraordinarily slower than that of a for-loop (it will be slower but not significantly). With that said, you can use a forloop to handle this same logic as seen below : 

    for(int i = 0; i < rowsthree.Count(); i++)
    {
          //Rather than referencing by "row" as you did previously, you will use rowsthree[i]
          var row = rowsthree[i];
    }



    Wednesday, May 8, 2013 1:45 PM
  • User1519602350 posted

    Rion, As per your above code I have modified a little bit, but it throws an error,

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0841: Cannot use local variable 'rowsthree' before it is declared

    Source Error:

    Line 12: 
    Line 13:                     for(int i = 0; i < rows.Count(); i++){
    Line 14: var row = rowsthree[i];

     var db = Database.Open("sls");
        var rows = db.Query(@"SELECT postingID, title, shortdescription, SUBSTRING(description, 1, 100) as description FROM Posting " +
                    "LEFT OUTER JOIN Categories ON Posting.category = Categories.categoryID " +
                    "WHERE Categories.categoryID = 1 ORDER BY Posting.postingID DESC");
     
     
                        var data = "";
     
                        data+="<div id='resultone' class='col span_1_of_4' style='padding: 0.1px; '>";
     
                        for(int i = 0; i < rows.Count(); i++){
                        var row = rowsthree[i];
                         data +=
                                "<div style='padding:4%; box-shadow: 3px 1px 3px #b6b6b6; -webkit-box-shadow: 3px 0px 5px -2px #888; margin-bottom:14px;' class='section group siteimage'>"+
                "<div><h2><p style='white-space: normal;'><a data-ajax='false' style='color:#5a5a5a;' href=PostingDetails.cshtml/" +row.postingID + ">"+ row.title + "</a></p></h2>"+
                   "<a data-ajax='false' style='color:#5a5a5a;' href=PostingDetails.cshtml/" +row.postingID + ">"+
                                    String.Format("<p><img class='large-photo' alt='' src='{0}' /></p>",Href("~/Thumbnail", row.postingID, new { size="medium" })) +
                                    "</a>"+
    Wednesday, May 8, 2013 1:56 PM
  • User281315223 posted

    I had assumed that rowsthree was the name of your "rows" collection.

    Based on the code that you provided you would want to just use "rows" instead : 

    for(int i = 0; i < rows.Count(); i++){
          //Refer to your row here
          var row = rows[i];
    }
    Wednesday, May 8, 2013 2:31 PM
  • User1519602350 posted

    Thanks Rion. Am getting the below error now

    Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<dynamic>'

    Source Error:

    Line 15:                     for(int i = 0; i < rows.Count(); i++){
    Line 16:                       //Refer to your row here
    Line 17: var row = rows[i]; Line 18:                 
    Wednesday, May 8, 2013 2:33 PM
  • User-1716253493 posted
    if you want data manipulation, i think in rowdatabound event will better performance instead looping all rows.
    Wednesday, May 8, 2013 9:22 PM
  • User309602797 posted

    if you are using > 3.5 fraimework then use

    Parallel.ForEach

    http://codingcramp.blogspot.in/2011/02/c-parallelforeach-simple-example.html

    Thursday, May 9, 2013 1:04 AM
  • User281315223 posted

    I'm not sure why you would be continually encountering performance issues based on the type of loop that you are using. The performance of foreach and a for loop should be relatively close, which leads me to believe that the issue could actually be related to your database query that may be slowing things down.

    Are you recieving any intellisense when you are using the following code : 

    for(int i = 0; i < rows.Count(); i++){
          //Check the following for intellisense
          var row = rows[i];
    }

    As the Database.Query() method that you are using returns an IEnumerable<Object> so you should be able to access the values accordingly. You may want to try not using var and instead using an object instead to see if that makes any difference and you should be able to refer to your explicit columns as well :

    for(int i = 0; i < rows.Count(); i++){
    //Example of using an object
    object row = rows[i];
    //Example of explicitly trying to access a row property var property = rows[i]["Your Column Name"]; }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2013 8:00 AM
  • User-1800438376 posted

    foreach(var row in rowsthree){} affects the performance of loading time, instead of foreach() what can i use?

    annonmous keyword 'var' degrade performance because it need to type cast to specified value at runtime.

    Thursday, May 9, 2013 8:24 AM
  • User-821857111 posted

    annonmous keyword 'var' degrade performance because it need to type cast to specified value at runtime.

    That is not correct. The type that var represents is determined at compile time, not runtime.

    Thursday, May 9, 2013 8:29 AM
  • User-1800438376 posted

    Mikesdotnetting

    That is not correct. The type that var represents is determined at compile time, not runtime.

    I misunderstood from the post that I read. thanks for showing me right direction

    Thursday, May 9, 2013 8:47 AM