1

我试图弄清楚如何使用 Linq 执行子选择。我有一个带有“借方”和“贷方”列的 Excel 表格。我需要过滤掉任何具有与进一步向下的贷方列值匹配的借方列值(> 0.00)的行。两行必须具有相同的付款人 ID。到目前为止,这是我想出的:

    public void balanceSheet()
    {
        foreach (Payment payment in this.payments)
        {
            // c[6] is the payers ID.
            var debits = from c in this.test.WorksheetNoHeader()
                         where c[6] != "0" && c[13] != "0.00" 
                         select c;
            // Find any rows in the sheet that have the same payer id AND the debit 
            // amount from the query above in it's credit column.
            foreach(LinqToExcel.RowNoHeader debit in debits)
            {
                var credits = from c in this.test.WorksheetNoHeader()
                              where c[6] == debit[6] && c[15] == debit[13]
                              select c;

                // Do something awesome if it finds something.

            }
        }
    }

我希望有一种更优雅的解决方案来按上述标准选择 excel 行,而不是每次都循环遍历它们。我不认为我正在使用 LINQ 来发挥它的全部潜力。有任何想法吗?

4

2 回答 2

2

LinqToExcel 不支持连接,但可以将其转换为内存列表,然后执行连接

var credits = from credit in this.test.WorksheetNoHeader().ToList()
  join debit in this.test.WorksheetNoHeader().ToList() on credit[6] equals debit[6]
  where debit[13] != "0.00"
  where debit[13] == credit[15]
  select credit
于 2012-02-05T02:24:47.177 回答
1

尝试加入:

var credits = from credit in this.test.WorksheetNoHeader()
  join debit in this.test.WorksheetNoHeader() on credit[6] equals debit[6]
  where debit[13] != "0.00"
  where debit[13] == credit[15]
  select credit

欲了解更多信息:http: //msdn.microsoft.com/en-us/library/bb311040.aspx

于 2012-02-04T16:46:03.033 回答