我将2012年与LINQ中的DateTime年份整数进行比较,以及年份= 2013年的LINQ返回行。我想知道我做错了什么。
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();
}
答案 0 :(得分: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);
答案 1 :(得分: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);