Q: Get all if variable is null else get matching Can't figure out the logic for Get all records if variable is null else get where 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();


A: While you can make a compound if statement, this will (usually -- currently always) pass this compound statement on to the database.  In many cases, this can cause index misses which if done correctly wouldn't be missed.  In non-Microsoft SQL servers, these types of queries are also known to just plain not work (MySQL, DB2).  It's better to just write the query correctly in the first place:
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)
                       });


A: Just swap ("Get all records") with (officer == null), Although i would suggest you to put this condition at first.
Explanation:
If officer == null than respect to short circut law we will return true and the rest of our condition won't matter.
Else means officer != null and thus we want to check if it matches our h.OfficerName data.
End result:
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();


A: Have you tried: 
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();

