Sunday, September 11, 2011

I'm still fascinated by ReSharper's ability to refactor foreach loops into lambda expressions.

This...

foreach (DataRow dataRow in myDataTable.Rows)

{

   if (dataRow[FindBy].ToString().ToLower().Contains(FindText.ToLower()))

   {

      myStringList.Add(dataRow["Name"].ToString());

   }

}

 
 

May become...

foreach (DataRow dataRow in myDataTable.Rows.Cast<DataRow>().Where(dataRow => dataRow[FindBy].ToString().ToLower().Contains(FindText.ToLower()))) myStringList.Add(dataRow["Name"].ToString());

No comments:

Post a Comment