使用LINQ查询DataTable

时间:2012-05-02 16:18:39

标签: c# asp.net linq

我的C#linq查询有点麻烦,我希望你能提供帮助。

我想要做的是从数据库中获取货币,日期和汇率列表,然后将记录转换为GBP,EUR或USD,并转换为单独的表格。我知道我可以使用多个foreach / for循环执行此操作,但我希望在没有这些多次迭代的情况下执行此操作。

我目前获得gbpConvertedRates的美元和欧元汇率(这很好),eurConvertedRates的美元(此处也需要GBP)以及除usdConvertedRates的EUR和GBP之外的所有其他汇率(也需要这两个汇率)< / p>

这是我的代码:

foreach (string curr in ConfigTemplate.WantedCurrencies)
{         
    temp = new DataTable();
    cmd = new SqlCommand();
    cmd.CommandText = "GetLast5Rates";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter param = new SqlParameter();
    param.ParameterName = "@curr";
    param.Value = curr;
    cmd.Parameters.Add(param);
    temp = dbConn.ExecuteQuery(cmd);
    MaxDate = DateTime.Parse(temp.Compute("MAX(Date)", string.Empty).ToString());
    MinDate = DateTime.Parse(temp.Compute("MIN(Date)", string.Empty).ToString());
    OtherDate = MaxDate.AddDays(-1);
    //Populate the base rates for EUR, GBP and USD
    for (int i = 0; i < temp.Rows.Count; i++)
    {
        if (curr == "EUR")
        {
            if (log.IsInfoEnabled) log.Info("Populate EUR DataTable");
            row = eurRateDt.NewRow();
            row[1] = DateTime.Parse(temp.Rows[i].ItemArray[0].ToString());
            row[0] = temp.Rows[i].ItemArray[1].ToString();
            row[2] = Convert.ToDecimal(temp.Rows[i].ItemArray[2]);
            eurRateDt.Rows.Add(row);
        }
        if (curr == "GBP")
        {
            if (log.IsInfoEnabled) log.Info("Populate GBP DataTable");
            row = gbpRateDt.NewRow();
            row[1] = DateTime.Parse(temp.Rows[i].ItemArray[0].ToString());
            row[0] = temp.Rows[i].ItemArray[1].ToString();
            row[2] = Convert.ToDecimal(temp.Rows[i].ItemArray[2]);
            gbpRateDt.Rows.Add(row);
        }
        if (curr == "USD")
        {
            if (log.IsInfoEnabled) log.Info("Populate USD DataTable");
            row = usdRateDt.NewRow();
            row[1] = DateTime.Parse(temp.Rows[i].ItemArray[0].ToString());
            row[0] = temp.Rows[i].ItemArray[1].ToString();
            row[2] = Convert.ToDecimal(temp.Rows[i].ItemArray[2]);
            usdRateDt.Rows.Add(row);
        }
    }
    //Linq query that converts the rates in the temp table
    //to that of the GBP rates 
    var gbpConversion = from myRow in temp.AsEnumerable()
                        join gbpRow in gbpRateDt.AsEnumerable() 
                            on myRow["Date"] equals gbpRow["Date"]
                        where myRow["Currency"].ToString() == "USD"
                            || myRow["Currency"].ToString() == "EUR"
                        select new
                               {
                                   Currency = myRow["Currency"],
                                   Date = myRow["Date"],
                                   Rate = myRow.Field<Decimal>("Rate") / gbpRow.Field<Decimal>("Rate")
                               };

    var eurConversion = from myRow in temp.AsEnumerable()
                        join eurRow in eurRateDt.AsEnumerable() 
                            on myRow["Date"] equals eurRow["Date"]
                         //join gbpRow in gbpRateDt.AsEnumerable() on myRow["Date"] equals gbpRow["Date"]
                        where myRow["Currency"].ToString() == "GBP"
                            || myRow["Currency"].ToString() == "USD"
                        select new
                               {
                                   Currency = myRow["Currency"],
                                   Date = myRow["Date"],
                                   Rate = myRow.Field<Decimal>("Rate") / eurRow.Field<Decimal>("Rate")
                               };

    var usdConversion = from myRow in temp.AsEnumerable()
                        join usdRow in usdRateDt.AsEnumerable() 
                            on myRow["Date"] equals usdRow["Date"]
                        where myRow["Currency"].ToString() != "USD"
                        select new
                               {
                                   Currency = myRow["Currency"],
                                   Date = myRow["Date"],
                                   Rate = myRow.Field<Decimal>("Rate") / usdRow.Field<Decimal>("Rate")
                               };
    usdConvertedRates = usdConversion.CopyToDataTable(usdConvertedRates, LoadOption.PreserveChanges);
    gbpConvertedRates = gbpConversion.CopyToDataTable(gbpConvertedRates, LoadOption.PreserveChanges);
    eurConvertedRates = eurConversion.CopyToDataTable(eurConvertedRates, LoadOption.PreserveChanges);
}

0 个答案:

没有答案