我想使用lambda表达式从集合列表中过滤数据。但是,使用Fromdate和Todate时,数据无法正确过滤。它只会过滤帐户ID,而不会过滤日期。
这是我的lambda表达式:
var query = context.Accounts
.Include(s => s.Sales)
.Where(a => a.Type == "C" && a.Sales.Count != 0).AsQueryable();
if (filter.AccountId.HasValue)
{
query = query
.Where(a => a.Sales.Any(s => (s.AccountId == filter.AccountId))).AsQueryable();
query = query.Where(a => a.Sales.Any(s => s.TransactionDate >= filter.FromDate.Date && s.TransactionDate <= filter.ToDate.Date));
}
if (!filter.AccountId.HasValue && (filter.FromDate.Date != DateTime.Now.Date || filter.ToDate.Date != DateTime.Now.Date))
{
query = query
.Where(a => a.Sales.Any(s => s.TransactionDate >= filter.FromDate.Date && s.TransactionDate <= filter.ToDate.Date));
}
return await query.ToListAsync();