0

我编写了一些代码,将两个不同的文件组合到一个文件中,然后它比较来自匹配 seqNum 的数据,并对它们做一些数学方程式。我遇到的问题是用我从数学部分得到的答案打印出每个 seqNum 的所有其他匹配数据。我使用 LINQ 来匹配两个不同的 seqNum 数组,然后编写了一个 while 循环来打印出所有数据。当每次我在变量“item”(数学方程式的答案)上放置一个 int 值时,我都会得到一个超出范围的异常,但是当我没有在“item”上放置一个 int 值时,它会循环并输出相同的答案对于我在数组中的每个项目,然后转到下一个答案并做同样的事情。这是代码...

    private void executeBtn_Click(object sender, EventArgs e)
    {

        //NET OIL VARIANCE MATHEMATICS
        if (netOilRadBtn.Checked)
        {
            using (var sw = new StreamWriter("testNetOil.csv"))
            {
                var items = netOil.Zip(seqNum, (oil, seq) => new { Oil = oil, Seq = seq });
                var items2 = netOil2.Zip(seqNum2, (oil, seq) => new { Oil = oil, Seq = seq });
                sw.WriteLine("Lease Name, Field Name, Reservoir, Operator, County, ST, Majo, Resv Cat, Discount Rate, Net Oil Interest, Net Gas Interest, Working Interest, Gross Wells, Ultimate Oil, Ultimate Gas, Gross Oil, Gross NGL, Gross Gas, Net Oil, Net Gas, Net NGL, Revenue To Int., Oper. Expense, Total Invest., Revenue Oil, Revenue Gas, Operating Profit, Revenue NGL, Disc Net Income, SEQ, Well ID, INC ASN, Life Years, Own Qual, Production Tax, NET OIL VARIANCE");

                foreach (var item in items.Join(items2, i => i.Seq, i => i.Seq, (a, b) =>
                {
                    double first = Convert.ToDouble(a.Oil);
                    double second = Convert.ToDouble(b.Oil);
                    double answer = (first - second) / second;
                    return string.Format("{0}, {1}", a.Seq, answer);
                }))
                {                        
                    int x = listHead;
                    while (x != -1)
                    {
                        sw.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}, {33}, {34}, {35}",
                QuoteString(leaseName[x]), fieldName[x], QuoteString2(reservoir[x]), operator1[x], county[x], state[x], majo[x], resvCatgory[x], disRate[x], netOil2Int[x], netGas2Int[x], workingInt[x], grossWells[x]
                , ultOil[x], ultGas[x], grossOil[x], grossNGL[x], grossGas[x], netOil[x], netGas[x], netNGL[x], revToInt[x], operExpense[x], totInvest[x], revOil[x], revGas[x], operatingProfit[x],
                revNGL[x], discNetIncome[x], seqNum[x], wellID[x], incASN[x], lifeYears[x], ownQual[x], prodTax[x], item[x]);
                x = pointers[x];

                        //sw.WriteLine(item);
                    }
                    sw.Close();
                }
            }
        }

我只是想知道是否有人可以帮助我解决这个问题,并且只需将答案输出到正确的 seqNum 以及所有其他匹配的数据数组。谢谢。

4

1 回答 1

0

好的,首先我将计算逻辑移到一个方法中以使其更清晰,因此您需要添加以下内容:

private double GetTheAnswer(double first, double second)
{
    double answer = (first - second) / second;
    return answer;
}

然后我修改了您的 LINQ 语句并将您感兴趣的位选择到一个匿名对象中。之后,我在输出写入语句的末尾添加了 1 个字段,然后访问了我针对匿名类型定义的属性。我还删除了针对项目的数组索引“[x]”:

        using (var sw = new StreamWriter("testNetOil.csv"))
        {
            var items = netOil.Zip(seqNum, (oil, seq) => new { Oil = oil, Seq = seq });
            var items2 = netOil2.Zip(seqNum2, (oil, seq) => new { Oil = oil, Seq = seq });
            sw.WriteLine("Lease Name, Field Name, Reservoir, Operator, County, ST, Majo, Resv Cat, Discount Rate, Net Oil Interest, Net Gas Interest, Working Interest, Gross Wells, Ultimate Oil, Ultimate Gas, Gross Oil, Gross NGL, Gross Gas, Net Oil, Net Gas, Net NGL, Revenue To Int., Oper. Expense, Total Invest., Revenue Oil, Revenue Gas, Operating Profit, Revenue NGL, Disc Net Income, SEQ, Well ID, INC ASN, Life Years, Own Qual, Production Tax, NET OIL VARIANCE");

            foreach (var item in items.Join(items2, i => i.Seq, i => i.Seq, (a, b) => new { SeqID = a.Seq, Answer = this.GetTheAnswer(Convert.ToDouble(a.Oil), Convert.ToDouble(b.Oil)) }))
            {                        
                int x = listHead;
                while (x != -1)
                {
                    sw.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}, {33}, {34}, {35}, {36}",
            QuoteString(leaseName[x]), fieldName[x], QuoteString2(reservoir[x]), operator1[x], county[x], state[x], majo[x], resvCatgory[x], disRate[x], netOil2Int[x], netGas2Int[x], workingInt[x], grossWells[x]
            , ultOil[x], ultGas[x], grossOil[x], grossNGL[x], grossGas[x], netOil[x], netGas[x], netNGL[x], revToInt[x], operExpense[x], totInvest[x], revOil[x], revGas[x], operatingProfit[x],
            revNGL[x], discNetIncome[x], seqNum[x], wellID[x], incASN[x], lifeYears[x], ownQual[x], prodTax[x], item.SeqID, item.Answer);
            x = pointers[x];

                    //sw.WriteLine(item);
                }
                sw.Close();
            }
        }

显然我无法为你测试这个,但希望这会让你走上正确的道路,我得到了正确的结果!

于 2013-06-06T16:28:13.240 回答