简体   繁体   English

如何从强类型数据集中的数据表中获取不同的值

[英]How to get distinct values from a datatable in strongly typed dataset

I am using strongly typed dataset and have many tables in that.. 我正在使用强类型数据集,并且其中有很多表。

The problem is now i want to filter data from 现在的问题是我想从中过滤数据

GetData()

function which has a query like 具有类似查询的功能

select * from table_name

How can i filter a particular table and distinct values from it. 我如何过滤特定的表并从中区分出不同的值。 Also if i try to filter it return all column but rest have null values except the one i asked, so i cannot assign it as a datasource to a datagrid or combobox 另外,如果我尝试对其进行过滤,则返回所有列,但其余字段为空值,但我要求的除外,因此我无法将其作为数据源分配给datagrid或combobox

How can i do this.. 我怎样才能做到这一点..

Your question is not very clear. 您的问题不是很清楚。 What I have understood is, you have multiple tables coming in a dataset. 我了解的是,数据集中有多个表。 Now you want to filter based about Table Name. 现在,您要根据表名称进行过滤。 If you are returning multiple tables in dataset by writing multiple select queries in a single stored procedure, then there is no way you could name the tables in sql. 如果要通过在单个存储过程中编写多个选择查询来返回数据集中的多个表,则无法用sql命名表。 You have to access it by hard coded way. 您必须通过硬编码方式访问它。 Another way could be you could add a table at 0th position and in that table add the name of the table and position that it would be in DataSet while returning the query. 另一种方法是,您可以在第0个位置添加一个表,然后在该表中添加该表的名称和返回查询时该表在DataSet中的位置。 So the first query in your stored procedure would return a table which has mapping between name of the table and postion they are in DataSet. 因此,存储过程中的第一个查询将返回一个表,该表在表名和它们在DataSet中的位置之间具有映射。 Now GetData() function would become very easy. 现在,GetData()函数将变得非常容易。

function DataTable GetData(string tableName)
{
  //Supposing 0th table is mapping table with 2 columns, One contains Name and another   position
   var pos = ds.Tables[0].where(x => x[0] == tableName).Select(x => x[1]).firstOrDefault();
   var table = ds.Tables[pos];
   return table;
}

How about the Select() method of the DataTable? 数据表的Select()方法怎么样?

DataRow[] filtered = someDataSet.SomeDataTable.Select("Status = 'Active'");

Edit: 编辑:
Updated code sample following OP's comment OP评论后更新了代码示例

using System.Linq;
...
DataRow[] rows = someDataSet.SomeDataTable.Select("Status = 'Active'");
string[] columnValues = row.Select(x => x["SomeColumnName"].ToString());

Note, the two Select() methods are different. 注意,两个Select()方法是不同的。 The first is a DataTable method that filters rows. 第一个是用于过滤行的DataTable方法。 The second is a linq extension method that is transforming an array of rows into an array of strings. 第二种是linq扩展方法,它将行的数组转换为字符串的数组。

As I understood your question, I did something quick to try to help, the code can be improved and must be, how I said I did it fast. 当我理解您的问题时,我做了一些快速的尝试来帮助您,代码可以得到改进,而且必须如此,我说我做得很快。

Public Module DataSetExtensions
    <Runtime.CompilerServices.Extension()>
    Public Function [Select](ds As DataSet, table As String, ParamArray campos() As String) As DataTable
        Dim dt As New DataTable
        Dim sourceTable = (From t As DataTable In ds.Tables _
            Where t.TableName = table).SingleOrDefault

        Dim columnas = From c As DataColumn In sourceTable.Columns Where campos.Contains(c.ColumnName)

        columnas.ToList.ForEach(Sub(c) dt.Columns.Add(c.ColumnName))

        For Each row As DataRow In sourceTable.Rows
            Dim newRow As DataRow = dt.NewRow
            For Each col As DataColumn In sourceTable.Columns
                If columnas.Contains(col) Then
                    newRow(col.ColumnName) = row(col)
                End If
            Next
            dt.Rows.Add(newRow)
        Next

        Return dt
    End Function

    <Runtime.CompilerServices.Extension()>
    Public Function [Select](table As DataTable, ParamArray campos() As String) As DataTable
        Dim dt As New DataTable

        Dim columnas = From c As DataColumn In table.Columns Where campos.Contains(c.ColumnName)

        columnas.ToList.ForEach(Sub(c) dt.Columns.Add(c.ColumnName))

        For Each row As DataRow In table.Rows
            Dim newRow As DataRow = dt.NewRow
            For Each col As DataColumn In table.Columns
                If columnas.Contains(col) Then
                    newRow(col.ColumnName) = row(col)
                End If
            Next
            dt.Rows.Add(newRow)
        Next

        Return dt
    End Function
End Module

And the call something like this 像这样的电话

Using ds As New DataSet1()
    Using ta As New DataSet1TableAdapters.BCR_SOLICITUDTableAdapter()
        ta.Fill(ds.BCR_SOLICITUD)
        Dim dt As DataTable
        ' First extended method
        dt = ds.Select("BCR_SOLICITUD", "Numero", "Estado", "Descripción")
        ' Second extended method
        dt = ds.BCR_SOLICITUD.Select("Numero","Estado", "Descripción")
        'Code here
        dt.Dispose
        dt=Nothing
    End Using
End Using

You could use using in DataTable, but this is not the topic. 您可以在DataTable中使用using,但这不是主题。 I hope it help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM