如果变量为null则获取all,否则获得匹配

时间:2015-08-13 22:16:20

标签: c# sql linq

如果变量为null,则无法找出获取所有记录的逻辑,否则获取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();

3 个答案:

答案 0 :(得分:1)

虽然您可以创建一个复合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)
                       });

答案 1 :(得分:0)

只需将("Get all records")(officer == null)交换,虽然我建议您先放入此条件。

<强>解释

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

否则表示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();

答案 2 :(得分:0)

你试过了吗?

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();