【问题标题】:Extracting data from datatable with conditions c#使用条件c#从数据表中提取数据
【发布时间】:2017-02-17 21:14:47
【问题描述】:

我有一个数据表,其中包含来自不同用户的信息,每个数据的用户都存储为我的数据表中的一列。

只有当用户 = msalmon 而不是像 John 这样的人时,我才能提取相关数据以显示在 datagridview 中?

我的桌子:

【问题讨论】:

  • var query=table.AsEnumerable().Where(r=>r.Field<string>("SessionUName")=="msalmon");
  • 当我用我的数据表名称替换“table”时,“AsEnumerable”下出现错误行:/
  • 您需要导入System.Linq 并添加对System.Data.DataSetExtensions dll 的引用。 Read

标签: c# datatable conditional-statements


【解决方案1】:

您可以执行以下操作:

private void GetRowsByFilter()
{
    DataTable yourDataTable = new DataTable(); //Your DataTable is supposed to have the data
    // Presuming the DataTable has a column named user.
    string expression;
    expression = "user = \"msalmon\"";
    DataRow[] foundRows;

    // Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression);

    // Print column 0 of each returned row.
    for(int i = 0; i < foundRows.Length; i ++)
    {
        Console.WriteLine(foundRows[i][0]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多