Hi,
I am having this For each loop
foreach (Account acct in acctTest)
{
if (acct.AccountId == acctId)
{
foreach (Customer cust in acct.CustomerColl)
{
if (cust.CustomerId == custId)
{
customerName = cust.CustomerName;
break;
}
}
}
}
Linq query which does similar stuff (think this can be improved)
customerName = (from acct in acctTest
where acct.AccountId == acctId
from cust in acct.CustomerColl
where cust.CustomerId == custId
select cust.CustomerName).ToString() ;
I execute the above two in a loop of 1000 times for 5 times, get the execution timings as below.
Elapsed time for For Each ::: 7377
Elapsed time for Linq2 ::: 15653
Elapsed time for For Each ::: 1576
Elapsed time for Linq2 ::: 1718
Elapsed time for For Each ::: 1569
Elapsed time for Linq2 ::: 1726
Elapsed time for For Each ::: 1569
Elapsed time for Linq2 ::: 5583
Elapsed time for For Each ::: 1570
Elapsed time for Linq2 ::: 1506
why is there a difference and inconsistency in execution timings? Also , is there a way the LINQ query can be rewritten for better performance?