簡體   English   中英

執行數據表的聚合功能

[英]Perform Aggregate function of DataTable

我必須像Datatable.Compute一樣在DataTable上執行聚合函數,但計算返回的對象是我想在datatable上執行聚合函數並獲取datarow的對象。

_summaryTable.Compute("min(FareAdult)", whereClause 
                        + "AirlineDisplayName='" 
                        + Convert.ToString(airline["AirlineDisplayName"]) 
                        + "' and ( Stops=0) ");

但是上面的代碼只會返回min(FareAdult),但是我想根據上述條件從數據表中選擇兩列。

我如何通過Linq做到這一點,我必須選擇min(FareAdult)和同一行的TotelPrice

private void CalcColumns()
{
    DataTable table = new DataTable ();

    //enter code here

    // Create the first column.
    DataColumn priceColumn = new DataColumn();
    priceColumn.DataType = System.Type.GetType("System.Decimal");
    priceColumn.ColumnName = "price";
    priceColumn.DefaultValue = 50;

    // Create the second, calculated, column.
    DataColumn taxColumn = new DataColumn();
    taxColumn.DataType = System.Type.GetType("System.Decimal");
    taxColumn.ColumnName = "tax";
    taxColumn.Expression = "price * 0.0862";

    // Create third column.
    DataColumn totalColumn = new DataColumn();
    totalColumn.DataType = System.Type.GetType("System.Decimal");
    totalColumn.ColumnName = "total";
    totalColumn.Expression = "price + tax";

    // Add columns to DataTable.
    table.Columns.Add(priceColumn);
    table.Columns.Add(taxColumn);
    table.Columns.Add(totalColumn);

    DataRow row = table.NewRow();
    table.Rows.Add(row);
    DataView view = new DataView(table);
    dataGrid1.DataSource = view;
}

使用Select代替compute

_summaryTable.Select("FilterationExpression");

DataRow[] dr = _summaryTable.Select("min(FareAdult),AirlineDisplayName='" + Convert.ToString(airline["AirlineDisplayName"]) + "' and ( Stops=0) ");

這是LINQ方法。 這是偽代碼,因為我不知道您的行的類型,並且我無法對其進行測試,但是想法是相同的。 使用LINQ選擇與您的條件匹配的行,按FareAdult排序,然后選擇第一個(最小)。

var minResult = (from row in _summaryTable.Rows
                where row.AirlineDisplayName == airline["AirlineDisplayName"] && row.Stops == 0
                orderby row.FareAdult
                select row).FirstOrDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM