Tuesday, April 26, 2016

I'm intrigued by .Cast

foreach (DataRow dataRow in dataTable.Rows)
{
   if (dataRow["User_Name"].ToString() == foo.UserName)
   {
      foo.UserId = Convert.ToInt32(dataRow["User_ID"]);
      break;
   }
}

 
 

ReSharper suggested that I refactor the C# above into a LINQ (Language Integrated Query) shape like so:

foreach (DataRow dataRow in dataTable.Rows.Cast<DataRow>().Where(dataRow =>
      dataRow["User_Name"].ToString() == foo.UserName))
{
   foo.UserId = Convert.ToInt32(dataRow["User_ID"]);
   break;
}

No comments:

Post a Comment