Rewrite foreach using lambda + c # 3.0
I am trying the following
foreach (DataRow dr in dt.Rows)
{
if (dr["TABLE_NAME"].ToString().Contains(sheetName))
{
tableName = dr["TABLE_NAME"].ToString();
}
}
using lambda like
string tableName = "";
DataTableExtensions.AsEnumerable(dt).ToList().ForEach(i =>
{
tableName = i["TABLE_NAME"].ToString().Contains(sheetName);
}
);
but getting a compile-time error "cannot implicitly bool to string". So how to achieve the same.?
thanks (C # 3.0)
+2
a source to share
1 answer
tableName
string
and Contains()
returns bool
.
So the error is due to
tableName = i["TABLE_NAME"].ToString().Contains(sheetName);
What can you do (but I think the best options are available in linq)
string tableName = "";
DataTableExtensions.AsEnumerable(dt).ToList().ForEach(i =>
{
var s = i["TABLE_NAME"];
if(s.ToString().Contains(sheetName))
tableName = s;
}
);
Good luck.
+2
a source to share