微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/98401.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐