0

我有两个文件被读入单独的数组,例如:

String[] leaseName2 = new String[1000];
String[] fieldName2 = new String[1000];
String[] reservoir2 = new String[1000];
String[] operator2 = new String[1000];
String[] county2 = new String[1000];
String[] state2 = new String[1000];
String[] majo2 = new String[1000];
String[] resvCatgory2 = new String[1000];
String[] netOil2 = new String[1000];
String[] netGas2 = new String[1000];
String[] netNGL2 = new String[1000];

String[] leaseName = new String[1000];
String[] fieldName = new String[1000];
String[] reservoir = new String[1000];
String[] operator1 = new String[1000];
String[] county = new String[1000];
String[] state = new String[1000];
String[] majo = new String[1000];
String[] resvCatgory = new String[1000];
String[] netOil = new String[1000];
String[] netGas = new String[1000];
String[] netNGL = new String[1000];

然后我使用 Linq 合并这两个文件,以合并两个匹配的数组,例如netOilnetOil2给我一个double答案。有一堆不同的数据行由相同的seqNum数组匹配。

String[] seqNum2 = new String[1000]; //This will be the identifier
String[] seqNum = new String[1000]; //This will be the identifier

我遇到的问题是使用标识数组输出所有相应的数据,例如leaseName[]andfieldname[]和 and 。这是我的 Linq 代码:reservoir[]seqNum

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


                //OilNum1 = a.Oil, GIVES FIRST OIL FROM NET OIL ARRAY 1, item.oilnum1 will print out first oil #
                //OilNum2 = b.Oil, GIVES SECOND OIL FROM NET OIL ARRAY 2, item.OilNum2 ********
            }))

            {

                sw.WriteLine(item.SeqID + "," + item.Answer); //this prints out the seqNum and Answer that I want to match with all of the other data in the arrays


                /*  commented out to see what I've tried
                int x = listHead;
                x.Equals(item.SeqID);
                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();
        }
    }

我无法打印出循环中匹配的所有数据seqNumforeach所以我尝试创建另一个循环,但打印出很多没有用的额外数据。如果有人知道如何seqNum在我得到AnswerLinq 代码后打印出所有数据,如果你能告诉我,我将不胜感激。任何帮助将不胜感激。

4

1 回答 1

4

我建议您在尝试使用 C# 和 LinQ 之前学习和练习一些基本的介绍性编程和 OOP。

  • 首先,拥有 11 个字符串数组的唯一想法令人作呕。您应该学习如何创建适当的数据模型并这样做。

    public class MyRecord
    {
        public int SeqNum {get;set;} // Notice the proper case in property names
        public string LeaseName {get;set;}
        public string FieldName {get;set;}
        public string Reservoir {get;set;}
        //... Etc.
    }
    
  • 然后,花点时间分析一下将所有属性都设为 . 是否是个好主意string。例如,如果一个属性只能有数字值,最好强键入它并使用intor double。如果一个属性只能有“真”或“假”值,那就是 a bool,依此类推。

我遇到的问题是使用标识 seqNum 数组输出所有相应的数据,例如leaseName[] 和fieldname[] 和reservoir[]。

那是因为您的数据是以最糟糕的方式建模的。如果你像上面那样创建一个类,你就不会有这个问题,因为与同一条记录有关的所有数据都将在这个类的同一个实例中。然后:

List<MyRecord> File1 {get;set;}
List<MyRecord> File2 {get;set;}

var myrecord = File1.FirstOrDefault(x => x.SeqNum == someSeqNum);
var leasename = myrecord.LeaseName;
var reservoir = myrecords.Reservoir;
//... etc

看看生活到底有多轻松?

  • 最重要的是,为了人类,请从 UI 背后的代码中删除您的业务逻辑。使用具有相关参数的相关方法创建适当的服务来执行此操作,以便隔离此功能并能够重用它。

    private void executeBtn_Click(object sender, EventArgs e)
    {
        MyService.ProcessRecords(relevant,parameters,from,the,UI);
    }  
    
于 2013-06-11T14:31:40.933 回答