利用DataView.ToTable 方法 (String) 在结果中查询,可以实现无限次循环

本文介绍了.NET Framework 2.0中新增的DataView.ToTable方法,该方法能够根据DataView中的行创建并返回一个新的DataTable。文章通过示例展示了如何使用此方法从原始数据创建筛选视图,并进一步生成包含筛选行的新DataTable。

 

BOOL PrepareTables(CString &output, CString &input)
            {
             if(m_second_query_check)
             {
              output=m_sTempTables[m_iCurTable];
              input=m_sTempTables[!m_iCurTable];
              m_iCurTable=!m_iCurTable;
             }
             else
             {
              output=m_sTable;
              input=m_sTempTables[m_iCurTable];
             }
             
             ////---------- 连接数据库------------///
             ////重新两表初始化
            return true; 
            }

 

 

DataView.ToTable 方法 (String) 

注意:此方法在 .NET Framework 2.0 版中是新增的。

根据现有 DataView 中的行,创建并返回一个新的 DataTable

命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)

语法语法
C#
public DataTable ToTable (
    string tableName
)

 

参数
tableName

返回的 DataTable 的名称。

 

 

返回值
一个新的 DataTable 实例,其中包含所请求的行和列。
备注备注

由于此方法不允许指定可用列的子集,因此输出表与输入表包含相同的列。

下面的控制台应用程序示例创建一个 DataTable,用数据填充 DataTable,并根据原始数据创建一个筛选视图,最后用一个新名称创建包含筛选出的行的 DataTable

private static void DemonstrateDataView()
{
    // Create a DataTable with three columns.
    DataTable table = new DataTable("NewTable");
    Console.WriteLine("Original table name: " + table.TableName);
    DataColumn column = new DataColumn("ID", typeof(System.Int32));
    table.Columns.Add(column);

    column = new DataColumn("Category", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("Product", typeof(System.String));
    table.Columns.Add(column);

    column = new DataColumn("QuantityInStock", typeof(System.Int32));
    table.Columns.Add(column);


    // Add some items.
    DataRow row = table.NewRow();
    row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
    table.Rows.Add(row);

    row = table.NewRow();
    row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
    table.Rows.Add(row);

    // Mark all rows as "accepted". Not really required
    // for this particular example.
    table.AcceptChanges();

    // Print current table values.
    PrintTableOrView(table, "Current Values in Table");

    DataView view = new DataView(table);
    view.RowFilter = "QuantityInStock > 15";
    PrintTableOrView(view, "Current Values in View");

    DataTable newTable = view.ToTable("FilteredTable");
    PrintTableOrView(newTable, "Table created from filtered DataView");
    Console.WriteLine("New table name: " + newTable.TableName);

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static void PrintTableOrView(DataView dv, string label)
{
    System.IO.StringWriter sw;
    string output;
    DataTable table = dv.Table;

    Console.WriteLine(label);

    // Loop through each row in the view.
    foreach (DataRowView rowView in dv)
    {
        sw = new System.IO.StringWriter();

        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(rowView[col.ColumnName].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    }
    Console.WriteLine();
}

private static void PrintTableOrView(DataTable table, string label)
{
    System.IO.StringWriter sw;
    string output;

    Console.WriteLine(label);

    // Loop through each row in the table.
    foreach (DataRow row in table.Rows)
    {
        sw = new System.IO.StringWriter();
        // Loop through each column.
        foreach (DataColumn col in table.Columns)
        {
            // Output the value of each column's data.
            sw.Write(row[col].ToString() + ", ");
        }
        output = sw.ToString();
        // Trim off the trailing ", ", so the output looks correct.
        if (output.Length > 2)
        {
            output = output.Substring(0, output.Length - 2);
        }
        // Display the row in the console window.
        Console.WriteLine(output);
    } //
    Console.WriteLine();
}

public static DataTable ConvertDataTable(DataTable SortedInfo_table)//将数据表转换为报警邮件相对应的格式(Device\WIP\已开站1\Total1) { var summary = from c in SortedInfo_table.AsEnumerable() orderby c.Field(“Tech”), c.Field(“Device”), c.Field(“WIP”), c.Field(“Priority”) group c by new { Tech= c.Field(“Tech”), Productname = c.Field(“Device”), WIP = c.Field(“WIP”) } into d select new { Tech= d.Key.Tech, Productname = d.Key.Productname, WIP = d.Key.WIP }; var PriInfo_table = new DataTable(); PriInfo_table.Columns.Add(“Tech”, typeof(string)); PriInfo_table.Columns.Add(“Productname”, typeof(string)); PriInfo_table.Columns.Add(“WIP”, typeof(decimal)); int i = 0; foreach (var item in summary) { PriInfo_table.Rows.Add(); PriInfo_table.Rows[i]["Tech"] = item.Tech; PriInfo_table.Rows[i]["Productname"] = item.Productname; PriInfo_table.Rows[i]["WIP"] = item.WIP; i++; }//得到前三列信息 PriInfo_table.Columns.Add("Total_1"); PriInfo_table.Columns.Add("ScanStep_future_1"); PriInfo_table.Columns.Add("Total_2"); PriInfo_table.Columns.Add("ScanStep_future_2"); PriInfo_table.Columns.Add("Total_3"); PriInfo_table.Columns.Add("ScanStep_future_3"); for (int t = 0; t < PriInfo_table.Rows.Count; t++) { DataRow dt = PriInfo_table.Rows[t]; var Device = dt["Productname"].ToString(); var WIP = dt["WIP"].ToString(); for (int c = 0; c < SortedInfo_table.Rows.Count; c++) { DataRow dc = SortedInfo_table.Rows[c]; if (dc["Device"].ToString() == Device && dc["WIP"].ToString() == WIP) { switch (dc["Priority"].ToString()) { case "1": dt["ScanStep_future_1"] = dc["ScanStep_future"].ToString(); dt["Total_1"] = dc["ScanStep"].ToString(); break; case "2": dt["ScanStep_future_2"] = dc["ScanStep_future"].ToString(); dt["Total_2"] = dc["ScanStep"].ToString(); break; case "3": dt["ScanStep_future_3"] = dc["ScanStep_future"].ToString(); dt["Total_3"] = dc["ScanStep"].ToString(); break; } } } } PriInfo_table.Columns.Add("F_Rate", typeof(string)).SetOrdinal(3); for (int t = 0; t < PriInfo_table.Rows.Count; t++) { DataRow df = PriInfo_table.Rows[t]; for (int j = 0; j < PriInfo_table.Columns.Count; j++) { if (df[j].ToString() == null|| df[j].ToString()=="") { df[j] = 0; } } var samplingscan = Convert.ToDecimal(df["ScanStep_future_1"]) + Convert.ToDecimal(df["ScanStep_future_2"]) + Convert.ToDecimal(df["ScanStep_future_3"]); var Total = Convert.ToDecimal(df["Total_1"]) + Convert.ToDecimal(df["Total_2"]) + Convert.ToDecimal(df["Total_3"]); //decimal rate = samplingscan / Total; //df["F_Rate"] = rate/*.ToString("P0")*/; df["F_Rate"] = samplingscan / Total; } var view = PriInfo_table.AsDataView(); view.Sort = PriInfo_table.Columns[0].ColumnName + "," + PriInfo_table.Columns[3].ColumnName + " DESC ," + PriInfo_table.Columns[2].ColumnName + " DESC "; PriInfo_table = view.ToTable(); List removeinfo_list = new List(); for (int j = 0; j < PriInfo_table.Rows.Count; j++) { DataRow dj = PriInfo_table.Rows[j]; //var Rate = Convert.ToString(dj[“Rate”]); //dj[“F_Rate”] = Rate.ToString(“P0”); if (Convert.ToDecimal(dj[“F_Rate”])== 0) { removeinfo_list.Add(dj); } else { decimal Rate = Convert.ToDecimal(dj[“F_Rate”]); dj[“F_Rate”] = Rate.ToString(“P0”); } } for (int b = 0; b < removeinfo_list.Count; b++) { PriInfo_table.Rows.Remove(removeinfo_list[b]); } //PriInfo_table_new.Columns.Remove("Rate"); return PriInfo_table; 上述c#代码中,F_rate代表什么,用途是什么
最新发布
11-21
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值