Answered by:
Remove empty values from DataTable using Linq

Question
-
Hi, I have a DataTable that returns me also Null and empty values from the SQL Server. How can I remove the Null and empty values with Linq and bind the result to the DropDown keeping the columns names of the DataTable?
DataView dv = getDataTable().DefaultView;
DataTable dtSociete = dv.ToTable(true, arrColumnsSociete);I tried that but it doenst recognize the fields name:
var dtSocieteNull = dtSociete.AsEnumerable().Where(r => r.ItemArray.Any(v => v != null && v != DBNull.Value));Thanks
Tuesday, November 26, 2013 10:35 AM
Answers
-
Hi,
According to your description, you want to remove empty values from DataTable using Linq.
I suggest that you can modify your code like this and do the test again:
var dtSocieteNull = dtSociete.AsEnumerable().Where(r => r.ItemArray.All(v => v != null && v != DBNull.Value));
Best regardsPatrick Liang
TechNet Community Support- Proposed as answer by Hemendra Agrawal Friday, December 6, 2013 9:49 AM
- Marked as answer by Dennis Guo Sunday, December 8, 2013 1:18 PM
Tuesday, December 3, 2013 11:53 AM -
Try this
dtSociete = dtSociete.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();
For more details check this out
http://stackoverflow.com/questions/7023140/how-to-remove-empty-rows-from-datatableRaghavendra Shanbhag | Blog: www.SharePointColumn.com
Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
Disclaimer: This posting is provided "AS IS" with no warranties.- Proposed as answer by Hemendra Agrawal Friday, December 6, 2013 9:49 AM
- Marked as answer by Dennis Guo Sunday, December 8, 2013 1:19 PM
Tuesday, December 3, 2013 1:49 PM
All replies
-
Hi,
According to your description, you want to remove empty values from DataTable using Linq.
I suggest that you can modify your code like this and do the test again:
var dtSocieteNull = dtSociete.AsEnumerable().Where(r => r.ItemArray.All(v => v != null && v != DBNull.Value));
Best regardsPatrick Liang
TechNet Community Support- Proposed as answer by Hemendra Agrawal Friday, December 6, 2013 9:49 AM
- Marked as answer by Dennis Guo Sunday, December 8, 2013 1:18 PM
Tuesday, December 3, 2013 11:53 AM -
Try this
dtSociete = dtSociete.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();
For more details check this out
http://stackoverflow.com/questions/7023140/how-to-remove-empty-rows-from-datatableRaghavendra Shanbhag | Blog: www.SharePointColumn.com
Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
Disclaimer: This posting is provided "AS IS" with no warranties.- Proposed as answer by Hemendra Agrawal Friday, December 6, 2013 9:49 AM
- Marked as answer by Dennis Guo Sunday, December 8, 2013 1:19 PM
Tuesday, December 3, 2013 1:49 PM