Replace empty cell with 0 using LINQ

Sure , i am done this using for each and while it is work fine but it is tack time so I think if I use the LINQ will be fast ,

@Dev2
I am not sure if you have shared the right project with me, as I dont see any logic/parts using any snippets from above suggestions.

But it looks to me that on some specific parts you want to overcome parsing an empty string to int issue.

Have a look on following trick:
grafik
Line1 / Line2 is show casing the fail of int parsing an empty string

Line 3 is using an object array (similar we have in datatables / cols) and sums up the values.
the empty values do not fail as we add an unobtrusive “0” to the item and prevents the conversions fail

1 Like

Way to Use Linq Method(c# code)

DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");

dt.Rows.Add(new[]{"1","A","40"});
dt.Rows.Add(new[]{"2","B",""});
dt.Rows.Add(new[]{"3","C"," "});
dt.Rows.Add(new[]{"4","D",null});

DataTable newdt = dt.AsEnumerable()
	.Select(row =>{
		DataRow newrow = dt.NewRow();
		newrow.ItemArray = row.ItemArray.Select(i => (i is null || i.ToString().Trim() == "" ? "0" : i)).ToArray();
		return newrow;
	}).CopyToDataTable();