Avoid enumerating SPListItemCollection object
Enumerating a SPListItemCollection object should be avoided for data querying purposes.
for e.g. in the following code is not optimal,
using(SPSite site = new SPSite(siteURL))
{
using(SPWeb web = site.OpenWeb())
{
SPList tasksList = web.Lists["Tasks"];
foreach(SPListItem item in tasksList.Items)
{
// Code
}
}
}
This is because each step will invoke a separate call to the DB. Instead the foreach loop should be used on a DataTable object which can derived as mentioned below. This will reduce multiple calls to the content DB with just one.
DataTable dt = tasksList.Items.GetDataTable();
foreach(DataRow dr in dt.Rows)
{
//code
}