试图用linq输出答案

时间:2013-06-10 14:29:17

标签: c# linq excel csv

我一直在研究一个程序,该程序通过一个名为“SeqID”的标识符连接两个数组并输出一个CSV文件的答案。

每当我将这个答案输出到一个文件时,它会为一堆行打印出相同的答案,然后最终移动到下一个答案并打印出一堆行等等。我认为循环中可能存在问题,但我没有看到它。这是代码......

    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) => new
                {SeqID = a.Seq, Answer = this.GetTheAnswer(Convert.ToDouble(a.Oil), Convert.ToDouble(b.Oil)) 

                }))

                {
                    foreach (var s in item.SeqID)
                    {
                        sw.WriteLine(item.Answer);
                    }

基本上我想让这段代码实现的只是在两个不同的数组中打印出匹配seqNum的的一个答案。

1 个答案:

答案 0 :(得分:2)

foreach (var s in item.SeqID)

循环遍历char中的每个SeqID。我无法想象这有用。只需删除循环,只执行sw.WriteLine(item.Answer)