i编写了一个Linq查询,该查询应该从asp.net核心中的表引发存储库返回记录列表。我一遍又一遍地扔代码,但没有发现任何错误。但查询不返回任何内容。
代码
`
public ListResultDto<RetrieveInvoiceRecoverLogOutput>RetrieveInvoiceRecoverLog(RetrieveInvoiceRecoverLogInput input)
{
try
{
bool searchById = input.Id.HasValue && input.Id != 0 ? true : false;
var invoiceRecoverLogs = _InvoiceRecoverLogRepository.GetAll()
.Where(x => searchById ? x.Id == input.Id : true)
.Where(x => !searchById && input.FromDate.HasValue ? x.TransactionDate.Date >= input.FromDate.Value.ToLocalTime().Date : true)
.Where(x => !searchById && input.ToDate.HasValue ? x.TransactionDate.Date <= input.ToDate.Value.ToLocalTime().Date : true)
.Where(x => !searchById && input.EmployeeId.HasValue && input.EmployeeId != 0 ? x.PaidBy == input.EmployeeId : true)
.Select(x => new RetrieveInvoiceRecoverLogOutput
{
Id = x.Id,
InvoiceInfoId = x.InvoiceInfoId,
TransactionDate = x.TransactionDate,
Description = x.Description,
PaidAmount = x.PaidAmount,
PaidBy = x.PaidBy,
PaidByName = x.PaidByUser.UserName,
CreatorUserId = x.CreatorUserId,
CreatorName = x.CreatorUserId == null ? "" : x.CreatorUser.UserName,
LastModifierUserId = x.LastModifierUserId,
LastModifierName = x.LastModifierUserId == null ? "" : x.LastModifierUser.UserName,
CreationTime = x.CreationTime,
LastModificationTime = x.LastModificationTime
})
.ToList();
return new ListResultDto<RetrieveInvoiceRecoverLogOutput>(ObjectMapper.Map<List<RetrieveInvoiceRecoverLogOutput>>(invoiceRecoverLogs));
}
catch(Exception ex)
{
throw new UserFriendlyException(ex.Message);
}
}
`
有人可以帮忙吗?
[我们知道LINQ
中的多个Where条件被logical AND
连接。在您的问题中,我们有4个条件,其中任何一个可能是问题的原因。
尝试一一删除条件,然后检查。我认为,第一个条件是导致问题的地方。
Where(x => searchById ? x.Id == input.Id : true)
使用SQL查询检查是否存在针对此输入ID的数据。