Reputation: 87
I have two files that are being read into separate arrays such as:
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];
I then merge the two files using Linq, to merge two of the matching arrays such as netOil
and netOil2
that give me a double
answer. There are a bunch of different rows of data that are matched by the same seqNum
arrays.
String[] seqNum2 = new String[1000]; //This will be the identifier
String[] seqNum = new String[1000]; //This will be the identifier
The problem I am having is outputting all of the corresponding data such as leaseName[]
and fieldname[]
and reservoir[]
with the identifying seqNum
array. Here is my Linq code:
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();
}
}
I am not able to print out all of the data with the matching seqNum
in the foreach
loop, so I tried to create another loop but that was printing out a lot of extra data that was not useful. If anyone has any idea on how to print out all of the data with the seqNum
after I get the Answer
with the Linq code, I would appreciate it if you could let me know. Any help would be greatly appreciated.
Upvotes: 0
Views: 97
Reputation: 44038
I would suggest that you learn and practice some basic intro programming and OOP before attempting to use C# and LinQ.
First of all, the sole idea of having 11 arrays of strings is disgusting. You should learn how to create a proper data model and do so.
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.
}
Then, take a second to analyze whether it is a good idea that ALL your properties be string
. For example, if a property can only have numeric values, it would be better that you strongly type that and use int
or double
. If a property can only have "true" or "false" values, that would be a bool
, and so on.
The problem I am having is outputting all of the corresponding data such as leaseName[] and fieldname[] and reservoir[] with the identifying seqNum array.
That's because your data is modeled in the worst possible way. If you create a class like above, you will not have that problem, because all data pertaining to the same record will be in the same instance of this class. Then:
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
See how easy life actually is?
To top it all, PLEASE, for the sake of mankind, REMOVE your business logic from the code behind the UI. Create proper SERVICES to do that, with relevant methods with relevant parameters, in order to isolate this functionality and be able to REUSE it.
private void executeBtn_Click(object sender, EventArgs e)
{
MyService.ProcessRecords(relevant,parameters,from,the,UI);
}
Upvotes: 4