Count distinct non-null rows in a datatable

痞子三分冷 提交于 2019-12-13 08:30:18

问题


I want to count the distinct values in a column of a datatable. I found this useful technique:

count = dataTable.DefaultView.ToTable(true, "employeeid").rows.count

This works fine if there are no nulls in the column, but if there are, the count is high by one. Is there a way to eliminate null valued rows from the count, or do I have to loop through the resulting table looking for a possible null?

tbl = dataTable.DefaultView.ToTable(true,"employeeid")
For Each row in tbl.rows
...

回答1:


You may require to take into a temporary storage. I'm not sure about the type but an example here..

var dataview = new DataView(dataTable);
dataview.RowFilter = "yourColumn != null";
var count = dataview.ToTable().Rows.Count;



回答2:


You can cast the DataRow collection in a DataTable to an IEnumerable and then use standard Linq to filter, group, and count the number of groups.

myDataTable.Rows.Cast<DataRow>()
            .Where(r => r.Field<int?>("EmployeeID").HasValue)
            .GroupBy(r => r.Field<int?>("EmployeeID"))
            .Count();



回答3:


DataTable table = new DataTable();
table.Columns.Add("employeeid", typeof(int));
table.Columns.Add("employeeid2", typeof(string));
table.Rows.Add(null, null);
table.Rows.Add(100, "50");
table.Rows.Add(103, "50");
table.Rows.Add(101, "50");
table.Rows.Add(102, "52");


// This is if the column employeeid is a nullable value type
var distinctCount = table.Rows.OfType<DataRow>().DistinctBy(x => x["employeeid"]).Count(x => x["employeeid"] != null);


// This is if the column employeeid is a string type
var distinctCount2 = table.Rows.OfType<DataRow>().DistinctBy(x => x["employeeid2"]).Count(x => !string.IsNullOrWhiteSpace(x["employeeid2"].ToString()));

This is utilizing the library MoreLinq. You can get this package by running this in your package manager console.

Install-Package morelinq -Version 1.4.0


来源:https://stackoverflow.com/questions/37491678/count-distinct-non-null-rows-in-a-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!