How to query a DataTable in memory to fill another data table

依然范特西╮ 提交于 2019-12-19 02:58:35

问题


I am trying to update a Microsoft report. What it does is write out how many clients where excluded from a conversion process and for what reason. Currently the program writes all of the deleted clients back to the server then queries it back to fill a specialty table with the results.

Here is the current query:

SELECT  DeletedClients.Reason, 
        COUNT(DeletedClients.Reason) AS Number, 
        CAST(CAST(COUNT(DeletedClients.Reason) AS float) 
            / CAST(t.Total AS float) 
            * 100 AS numeric(4, 1)) AS percentage
FROM DeletedClients CROSS JOIN
    (SELECT COUNT(*) AS Total
    FROM DeletedClients AS DeletedClients_1
    WHERE (ClinicID = @ClinicID)) AS t
WHERE (DeletedClients.ClinicID = @ClinicID) 
    AND (DeletedClients.TotalsIdent = @ident)
GROUP BY DeletedClients.Reason, t.Total
ORDER BY Number DESC

What I would like to do is not write DeletedClients to the server as it already exists in memory in my program as a DataTable and it is just slowing down the report and filling the database with information we do not need to save.

My main question is this, Either :

How do I query a data table to make a new in memory data table that has the same results as if I wrote out the the SQL server and read it back in with the query above?

OR

How in Microsoft Reports do you do a group by clause for items in a Tablix to turn =Fields!Reason.Value =Fields!Number.Value =Fields!percentage.Value into something similar to the returned result from the query above?


回答1:


You can use DataTable.Select to query the DataTable.

DataTable table = GetDataTableResults();
DataTable results = table.Select("SomeIntColumn > 0").CopyToDataTable();

Or for more complex queries, you can use LINQ to query the DataTable:

DataTable dt = GetDataTableResults();

var results = from row in dt.AsEnumerable()
              group row by new { SomeIDColumn = row.Field<int>("SomeIDColumn") } into rowgroup
              select new
              {
                  SomeID = rowgroup.Key.SomeIDColumn,
                  SomeTotal = rowgroup.Sum(r => r.Field<decimal>("SomeDecimalColumn"))
              };                    

DataTable queryResults = new DataTable();
foreach (var result in query)
    queryResults.Rows.Add(new object[] { result.SomeID, result.SomeTotal });



回答2:


There are two ways that I can think of to query the data table. Below is an example using both ways.

using System;
using System.Data;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            var deletedClients = GetDataTable();

            // Using linq to create the new DataTable.
            var example1 = deletedClients.AsEnumerable()
                                         .Where(x => x.Field<int>("ClinicId") == 1)
                                         .CopyToDataTable();

            // Using the DefaultView RowFilter to create a new DataTable.
            deletedClients.DefaultView.RowFilter = "ClinicId = 1";
            var rowFilterExample = deletedClients.DefaultView.ToTable();
        }

        static DataTable GetDataTable()
        {
            var dataTable = new DataTable();
            // Assumes ClinicId is an int...
            dataTable.Columns.Add("ClinicId", typeof(int));
            dataTable.Columns.Add("Reason");
            dataTable.Columns.Add("Number", typeof(int));
            dataTable.Columns.Add("Percentage", typeof(float));

            for (int counter = 0; counter < 10; counter++)
            {
                dataTable.Rows.Add(counter, "Reason" + counter, counter, counter);
            }

            return dataTable;
        }
    }
}


来源:https://stackoverflow.com/questions/7109268/how-to-query-a-datatable-in-memory-to-fill-another-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!