I don't know this is possible or not but let me ask you. How do I write the below loop in a shorter way using, for example, LINQ?
DataSet dsAllMonsters
List<string> lstAllMonsters
for (int i = 0; i < dsAllMonsters.Tables[0].Rows.Count; i++)
{
lstAllMonsters.Add(dsAllMonsters.Tables[0].Rows[i]["pokemonId"].ToString());
}
I think it could.
lstAllMonsters = dsAllMonsters.Tables[0].Rows
.Cast<DataRow>()
.Select(r => r["pokemonId"].ToString())
.ToList();
Yes, it can be done in one line:
lstAllMonsters = dsAllMonsters.Tables[0].Rows.Cast<DataRow>().Select(row => row["pokemonId"].ToString()).ToList();
But sometimes two lines are better than one. I think you will find this more readable:
var rows = dsAllMonsters.Tables[0].Rows.Cast<DataRow>();
lstAllMonsters = rows.Select(row => row["pokemonId"].ToString()).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With