0

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

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

我目前获得 gbpConvertedRates 的美元和欧元汇率(这很好),eurConvertedRates 的美元汇率(也需要英镑)以及除欧元和英镑的 usdConvertedRates 以外的所有其他汇率(也需要这两个汇率)

这是我的代码:

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);
}
4

0 回答 0