1

I am comparing 2012 with DateTime's Year integer in LINQ, and the LINQ return row with Year = 2013. I am wondering what wrong have I done.

var accountBalance_query = from report in context.AccountBalances
                           where report.FiscalYear.Year == reportYear &&
                           report.CompanyCode == companyCode &&
                           report.AccountNo == accountNo &&
                           (subLedgerTypeCode == null) ? report.SubLedgerTypeCode == null : report.SubLedgerTypeCode == subLedgerTypeCode &&
                           (subLedgerName == null) ? report.SubLedgerName == null : report.SubLedgerName == subLedgerName &&
                           report.AccountCurrencyCode == transactionCurCode
                           select report;

var reportCnt = accountBalance_query.Count();
if (reportCnt > 1)
{
    reason = "Find more than 1 account balance in database that match the following key. " +
        " CompanyCode = " + companyCode +
        " AccountNo = " + accountNo +
        " SubLedgerTypeCode = " + subLedgerTypeCode +
        " SubLedgerName " + subLedgerName +
        " Year " + reportYear +
        " CurrenyCode " + transactionCurCode;
    return false;
}

Model.GeneralLedger.AccountBalance accountBalance;
if (reportCnt == 1)
{
    accountBalance = accountBalance_query.First();
}

enter image description here

4

2 回答 2

1

尝试使用 lambda 表达式,看看会发生什么。一周前我有一些类似的查询,它正在使用 lambda 表达式。

context.AccountBalances.Where( report => report.FiscalYear.Year == reportYear &&
                       report.CompanyCode == companyCode &&
                       report.AccountNo == accountNo &&
                       (subLedgerTypeCode == null) ? report.SubLedgerTypeCode == null : report.SubLedgerTypeCode == subLedgerTypeCode &&
                       (subLedgerName == null) ? report.SubLedgerName == null : report.SubLedgerName == subLedgerName &&
                       report.AccountCurrencyCode == transactionCurCode);
于 2013-05-27T09:34:36.873 回答
0

找出原因。空检查条件需要另外两个括号

        var accountBalance_query = context.AccountBalances.Where(report => report.FiscalYear.Year == 2012 &&
                    report.CompanyCode == companyCode &&
                    report.AccountNo == accountNo &&
                    ((subLedgerTypeCode == null) ? report.SubLedgerTypeCode == null : report.SubLedgerTypeCode == subLedgerTypeCode) &&
                    ((subLedgerName == null) ? report.SubLedgerName == null : report.SubLedgerName == subLedgerName) &&
                    report.AccountCurrencyCode == transactionCurCode);
于 2013-05-27T09:47:33.650 回答