c# – 如何选择数据表中列的不同行数?

前端之家收集整理的这篇文章主要介绍了c# – 如何选择数据表中列的不同行数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个数据表:
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();
原文链接:https://www.f2er.com/csharp/98401.html

猜你在找的C#相关文章