LINQ Query - Replace NULL To Zero in DataTable

Posted by Naimishforu under VB.NET on 6/29/2011 | Points: 10 | Views : 7501 | Status : [Member] [MVP] | Replies : 2
Hi,

I have a DataTable in below format.

A B C D
1 1 1 1
2 2 2
3 3
4 4 4 4

I am using VB.NET as language.

I want a LINQ query which replaces the NULL values to 0 (Zero) in one shot. (or any easy way)

Any idea?

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/



Responses

Posted by: Lakn2 on: 6/29/2011 [Member] Starter | Points: 25

Up
0
Down
try this

in this link you can find null replacing with a string
http://stackoverflow.com/questions/1838232/c-linq-replacing-nulls-with-meaningful-string

Thanks&Regards
LakshmiNarayana Nalluri.

Naimishforu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/29/2011 [Member] Starter | Points: 25

Up
0
Down
var allrows=dt.AsEnumerable().Select(delegate(DataRow r)

{
DataRow nr = dt.NewRow();
foreach (DataColumn item in dt.Columns)
{
nr.SetField(item.ColumnName, r[item.ColumnName] is DBNull ? 0 : r[item.ColumnName]);
}
return nr;
}


Naimishforu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response