Use of For vs foreach while iterating EntityCollections

Posts   
 
    
prabhu
User
Posts: 77
Joined: 20-Dec-2006
# Posted on: 23-Oct-2007 15:34:34   

Hi,

I am having a doubt regarding the use of for vs foreach while iterating the entitycollection. In our scenario we are having a collection that contains m:n and m:m relationship between tables.

We are using DataTransfer objects as an container to transport data between client and webserver. In our BL Layer we are trying to populate the DTO collections by iterating the EntityCollection using foreach statement. Most of the examples in Documentation followed the for each style.

Please let me know whether i can use for or foreach while iterating the Entity collection.

Regards

Prabhu

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 24-Oct-2007 16:58:34   

Sure you can.

eg.

foreach(CustomerEntity customer in customersCollection)
{
    int x = customer.Id;
}
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 25-Oct-2007 16:50:02   

prabhu wrote:

Hi,

I am having a doubt regarding the use of for vs foreach while iterating the entitycollection. In our scenario we are having a collection that contains m:n and m:m relationship between tables.

We are using DataTransfer objects as an container to transport data between client and webserver. In our BL Layer we are trying to populate the DTO collections by iterating the EntityCollection using foreach statement. Most of the examples in Documentation followed the for each style.

Please let me know whether i can use for or foreach while iterating the Entity collection.

Prabhu

Where do you see a problem?

prabhu
User
Posts: 77
Joined: 20-Dec-2006
# Posted on: 26-Oct-2007 05:13:49   

Hi,

I am talking in performance point of view. What's the best way to fetch values from EntityCollection by using for or foreach statement.

Regards

Prabhu

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 26-Oct-2007 07:53:11   

prabhu wrote:

Hi,

I am talking in performance point of view. What's the best way to fetch values from EntityCollection by using for or foreach statement.

Regards

Prabhu

The difference probably isn't even measurable! This is a good example of premature optimization. The latency involved in the network transfer between your client and the webserver will far outweigh the nanosecond or two difference between For or ForEach.

Just use the easiest one in your particular case.

If performance is really a concern, look at how your data transfer objects will be serialized across the network - that is a more likely place to be able to find a saving.

Cheers Simon

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 26-Oct-2007 10:34:20   

In .net 1.x, foreach could lag behind for statements significantly in some cases. In .net 2.0 this isn't the case anymore. foreach is a tiny bit slower, but not much. (think one or two ms). As simon said: the network latency is way higher than whatever slowdown you'll ever encounter by using foreach.

Frans Bouma | Lead developer LLBLGen Pro
prabhu
User
Posts: 77
Joined: 20-Dec-2006
# Posted on: 29-Oct-2007 02:53:29   

Thanks for the reply...

prabhu
User
Posts: 77
Joined: 20-Dec-2006
# Posted on: 29-Oct-2007 03:00:49   

mihies wrote:

prabhu wrote:

Hi,

I am having a doubt regarding the use of for vs foreach while iterating the entitycollection. In our scenario we are having a collection that contains m:n and m:m relationship between tables.

We are using DataTransfer objects as an container to transport data between client and webserver. In our BL Layer we are trying to populate the DTO collections by iterating the EntityCollection using foreach statement. Most of the examples in Documentation followed the for each style.

Please let me know whether i can use for or foreach while iterating the Entity collection.

Prabhu

Where do you see a problem?

Hi Mihis,

I had a chance to go through your blog post titled "Implementing more useful tracing for LLBLGenPro 2.0". It is very useful for our team to debug the application. While enabling tracing by using the technique you had mentioned with verbose type(value = 4) we are getting sql with both formatted as well as the unformatted type in VS2005 output window. I need only the formatted sql statement to be written by the trace. Please help me on how to implement the same.

Regards

Prabhu