我有一个数据表:
DataTable table = new DataTable(); DataColumn column; column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "RelationshipTypeDescription"; table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "RelatedContactName"; table.Columns.Add(column);
我想知道DISTINCT COUNT OF COLUMN“RelationshipTypeDescription”.
我不确定如何在这里引用列名:
int relationshipCount = table.AsEnumerable().Distinct().Count();
有人可以帮我一把吗?
解决方法
你可以这样做:
int relationshipCount = table .AsEnumerable() .Select(r => r.Field<string>("RelationshipTypeDescription")) .Distinct() .Count();
但是你可能不需要调用AsEnumerable:
int relationshipCount = table .Select(r => r.Field<string>("RelationshipTypeDescription")) // Compiler error: "Cannot convert lambda expression to type 'string' because it is not a delegate type" .Distinct() .Count();