获取所有if变量为空,否则获取匹配

本文关键字:获取 if 变量 | 更新日期: 2023-09-27 18:07:47

如果变量为null,则无法找出Get所有记录的逻辑,否则获取officername = officer的位置

        var  res = (from h in db.BalanceHistories
                       where temp.Contains(h.LoanType ?? 0)
                       && ((officer != null && h.OfficerName.ToLower() == officer.ToLower()) || ("Get all records"))
                       group h by new { h.Date.Value.Month, h.Date.Value.Year } into p
                       select new
                       {
                           Month = p.Key.Month,
                           Year = p.Key.Year, 
                           Count = p.Count(),
                           Balance = p.Sum(x => x.Balance),
                           Delinquent = p.Sum(x => x.Delinquent)
                       }).ToList();

获取所有if变量为空,否则获取匹配

虽然您可以创建复合if语句,但这将(通常—目前总是)将该复合语句传递给数据库。在许多情况下,这可能导致索引丢失,如果操作正确,就不会丢失索引。在非microsoft SQL服务器中,这些类型的查询通常也无法正常工作(MySQL、DB2)。最好一开始就正确地编写查询:

var query = db.BalanceHistories
    .Where(h=>temp.Contains(h.LoanType ?? 0));
if (officer!=null)
{
   // Depending on your database, the ToLower()s here may not be needed.
   query=query.Where(h=>h.OfficerName.ToLower() == officer.ToLower()))
}
var res=(from h in query
         group h by new { h.Date.Value.Month, h.Date.Value.Year } into p
         select new
                       {
                           Month = p.Key.Month,
                           Year = p.Key.Year, 
                           Count = p.Count(),
                           Balance = p.Sum(x => x.Balance),
                           Delinquent = p.Sum(x => x.Delinquent)
                       });

("Get all records")(officer == null)交换,尽管我建议您将此条件放在首位。

解释:

如果officer == null短路规律,我们将返回true,其余的条件将无关紧要。

Else表示officer != null,因此我们要检查它是否与h.OfficerName数据匹配。

最终结果:

res = (from h in db.BalanceHistories
                   where temp.Contains(h.LoanType ?? 0)
                   && (
                        officer == null
                        || h.OfficerName.ToLower() == officer.ToLower())
                    )
                   group h by new { h.Date.Value.Month, h.Date.Value.Year } into p
                   select new
                   {
                       Month = p.Key.Month,
                       Year = p.Key.Year, 
                       Count = p.Count(),
                       Balance = p.Sum(x => x.Balance),
                       Delinquent = p.Sum(x => x.Delinquent)
                   }).ToList();

你试过了吗:

var  res = (from h in db.BalanceHistories
                   where temp.Contains(h.LoanType ?? 0)
                   && ((officer == null) || (h.OfficerName.ToLower() == officer.ToLower()))
                   group h by new { h.Date.Value.Month, h.Date.Value.Year } into p
                   select new
                   {
                       Month = p.Key.Month,
                       Year = p.Key.Year, 
                       Count = p.Count(),
                       Balance = p.Sum(x => x.Balance),
                       Delinquent = p.Sum(x => x.Delinquent)
                   }).ToList();