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];
}
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];
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"];
}
Member
229 Points
662 Posts
For each
May 08, 2013 01:16 PM|ssvikramuk|LINK
foreach(var row in rowsthree){} affects the performance of loading time, instead of foreach() what can i use?
Thanks
Contributor
4954 Points
1726 Posts
Re: For each
May 08, 2013 01:20 PM|thirumaran007|LINK
you could use for loop or while loop, foreach will be used to itterate some collections which use IEnum objects.
With Friendly,
Thirumaran
Member
229 Points
662 Posts
Re: For each
May 08, 2013 01:24 PM|ssvikramuk|LINK
i am using foreach() for to receive collection of data from the database. Is it possbile to use for loop for this?
All-Star
194009 Points
28028 Posts
Moderator
Re: For each
May 08, 2013 01:43 PM|Mikesdotnetting|LINK
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.
All-Star
114593 Points
18503 Posts
MVP
Re: For each
May 08, 2013 01:45 PM|Rion Williams|LINK
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 :
Member
229 Points
662 Posts
Re: For each
May 08, 2013 01:56 PM|ssvikramuk|LINK
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];
All-Star
114593 Points
18503 Posts
MVP
Re: For each
May 08, 2013 02:31 PM|Rion Williams|LINK
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 :
Member
229 Points
662 Posts
Re: For each
May 08, 2013 02:33 PM|ssvikramuk|LINK
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:
All-Star
52523 Points
15672 Posts
Re: For each
May 08, 2013 09:22 PM|oned_gk|LINK
Suwandi - Non Graduate Programmer
Participant
1300 Points
407 Posts
Re: For each
May 09, 2013 01:04 AM|akhleshchauhan|LINK
if you are using > 3.5 fraimework then use
http://codingcramp.blogspot.in/2011/02/c-parallelforeach-simple-example.html
Akhlesh Chauhan
All-Star
114593 Points
18503 Posts
MVP
Re: For each
May 09, 2013 08:00 AM|Rion Williams|LINK
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 :
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 :
Participant
786 Points
508 Posts
Re: For each
May 09, 2013 08:24 AM|luckyforu2006|LINK
annonmous keyword 'var' degrade performance because it need to type cast to specified value at runtime.
All-Star
194009 Points
28028 Posts
Moderator
Re: For each
May 09, 2013 08:29 AM|Mikesdotnetting|LINK
That is not correct. The type that var represents is determined at compile time, not runtime.
Participant
786 Points
508 Posts
Re: For each
May 09, 2013 08:47 AM|luckyforu2006|LINK
I misunderstood from the post that I read. thanks for showing me right direction