输出linq溢出异常

本文关键字:异常 溢出 linq 输出 | 更新日期: 2023-09-27 18:17:58

我试图用linq输出匹配数据的某个答案。下面是代码

    public string[] netoilVar(string[] final)
    {
        var items = netOil.Zip(seqNum, (oil, seq) => new {Oil = oil, Seq = seq });
        var items2 = netOil2.Zip(seqNum2, (oil, seq) => new { Oil = oil, Seq = seq });
        List<string> vars = new List<string>();
        foreach (var item in items2.Join(items, i => i.Seq, i => i.Seq, (a, b) => new
        {
            x = a.Seq,
            y = this.GetTheAnswer(Convert.ToDouble(a.Oil), Convert.ToDouble(b.Oil)),
            oilnum1 = a.Oil,
            oilnum2 = b.Oil,
        }))
        {
            vars.Add(item.y + "," + item.oilnum1 + "," + item.oilnum2);
            final = vars.ToArray();
        }
        return final;
    }
    //BEGINS THE EXECUTE BUTTON METHOD
    private void executeBtn_Click(object sender, EventArgs e)
    {
        //NET OIL VARIANCE MATHEMATICS
        if (netOilRadBtn.Checked)
        {
            int i = listHead;
            string[] x = new String[1000]; 
            StreamWriter sw = new StreamWriter("testNetOil.csv");
            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, Net Oil Variance., Current Year's Net Oil, Last Year's Net Oil");
            //Loops until the end of the list, printing out info
            while (i != -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}",
                    QuoteString(leaseName[i]), fieldName[i], QuoteString2(reservoir[i]), operator1[i], county[i], state[i], majo[i], resvCatgory[i], disRate[i], netOil2Int[i], netGas2Int[i], workingInt[i], grossWells[i]
                    , ultOil[i], ultGas[i], grossOil[i], grossNGL[i], grossGas[i], netOil[i], netGas[i], netNGL[i], revToInt[i], operExpense[i], totInvest[i], revOil[i], revGas[i], operatingProfit[i],
                revNGL[i], discNetIncome[i], seqNum[i], wellID[i], incASN[i], lifeYears[i], ownQual[i], netoilVar(x)[i]);
                i = pointers[i];
            }
            sw.Close();
        }

我在打印出所有数据的while循环上得到一个IndexOutOfRangeException,最具体地是在netoilVar(x)[I]部分。有没有办法让我得到正确的索引,这样就不会出现异常?

输出linq溢出异常

While循环可能很危险,正如您在这里看到的那样。我强烈建议您将数据抽象出来,以使工作更容易。

为数据建模:

public class MyData
{
    public string LeaseName { get; set; }
    public int UltOil { get; set; }
    public int UltGas { get; set; }

    public static string GetHeaders()
    {
        return "LeaseName, UltOil, UltGas";
    }
    public override string ToString()
    {
        return string.Join(",", LeaseName, UltOil, UltGas);
    }
}

当你写你的csv文件时,使用这样的模型:

private void Foo()
{
    //NET OIL VARIANCE MATHEMATICS
    if (netOilRadBtn.Checked)
    {
        var input = new List<MyData>(); // <-- Fill in your data here.
        StreamWriter sw = new StreamWriter("testNetOil.csv");
        sw.WriteLine(MyData.GetHeaders());
        //Loops until the end of the list, printing out info
        foreach (var item in input)
        {
            sw.WriteLine(item);
        }
        sw.Close();
    }
}

显然这是缩写,您将需要包括所有字段,名称并适当键入它们。