我有一些代码将两个数组与一个名为seqNum2
的标识符数组相结合。我无法用Linq组合第三个数组。我的代码中的第三个数组是grossNGL2
。当我将grossNGL2
从等式中取出时,它确实有效,但我需要在那里。这是代码:
private void grossOilMboeRadBtn_CheckedChanged(object sender, EventArgs e)
{
using (var sw = new StreamWriter("testUltOilMBOE.txt"))
{
var items = grossOil2.Zip(seqNum2, (goil, seq) => new { GOIL = goil, Seq = seq }); //goil STANDS FOR GROSS OIL
var items2 = grossGas2.Zip(seqNum2, (ggas, seq) => new { GGAS = ggas, Seq = seq }); //ggas STANDS FOR GROSS GAS
var items3 = grossNGL2.Zip(seqNum2, (gngl, seq) => new { GNGL = gngl, Seq = seq }); //gngl STANDS FOR GROSS NGL
//var olditems = ultOil.Zip(seqNum, (uoil, seq) => new { UOIL = uoil, Seq = seq });
//var olditems2 = ultGas.Zip(seqNum, (ugas, seq) => new { UGAS = ugas, Seq = seq });
foreach (var item in items.Join(items2, i => i.Seq, i => i.Seq, (a, b) =>
{
double first = Convert.ToDouble(a.GOIL);
double second = Convert.ToDouble(b.GGAS);
double third = Convert.ToDouble(c.GNGL);
double answer = first + third + (second / 6);
return string.Format("{0}, {1}", a.Seq, answer);
}))
{
sw.WriteLine(item);
}
如果有人可以告诉我如何将第三个数组添加到foreach
循环中,那将非常感激。
答案 0 :(得分:5)
对于连接,通常全面(或查询)语法更容易:
var query = from i1 in items
join i2 in items2 on i1.seq equals i2.seq
join i3 in items3 on i1.seq equals i3.seq
select new { i1, i2, i3 };
foreach(var x in query)
{
double first = Convert.ToDouble(x.i1.GOIL);
....