Example #1
0
 internal static Expression RecentContributionCount(
     ParameterExpression parm, CMSDataContext Db,
     int days,
     int fund,
     CompareType op,
     int cnt)
 {
     if (Db.CurrentUser == null || Db.CurrentUser.Roles.All(rr => rr != "Finance"))
         return AlwaysFalse(parm);
     var now = DateTime.Now;
     var dt = now.AddDays(-days);
     IQueryable<int> q = null;
     switch (op)
     {
         case CompareType.Greater:
             q = from c in Db.Contributions2(dt, now, 0, false, false, true)
                 where fund == 0 || c.FundId == fund
                 where c.Amount > 0
                 group c by c.CreditGiverId into g
                 where g.Count() > cnt
                 select g.Key ?? 0;
             break;
         case CompareType.GreaterEqual:
             q = from c in Db.Contributions2(dt, now, 0, false, false, true)
                 where fund == 0 || c.FundId == fund
                 where c.Amount > 0 || cnt == 0
                 group c by c.CreditGiverId into g
                 where g.Count() >= cnt
                 select g.Key ?? 0;
             break;
         case CompareType.Less:
             q = from c in Db.Contributions2(dt, now, 0, false, false, true)
                 where fund == 0 || c.FundId == fund
                 where c.Amount > 0
                 group c by c.CreditGiverId into g
                 where g.Count() < cnt
                 select g.Key ?? 0;
             break;
         case CompareType.LessEqual:
             q = from c in Db.Contributions2(dt, now, 0, false, false, true)
                 where fund == 0 || c.FundId == fund
                 where c.Amount > 0
                 group c by c.CreditGiverId into g
                 where g.Count() <= cnt
                 select g.Key ?? 0;
             break;
         case CompareType.Equal:
             if (cnt == 0) // This is a very special case, use different approach
             {
                 q = from pid in Db.Contributions0(dt, now, fund, 0)
                     select pid.PeopleId;
                 Expression<Func<Person, bool>> pred0 = p => q.Contains(p.PeopleId);
                 Expression expr0 = Expression.Invoke(pred0, parm);
                 return expr0;
             }
             q = from c in Db.Contributions2(dt, now, 0, false, false, true)
                 where fund == 0 || c.FundId == fund
                 where c.Amount > 0
                 group c by c.CreditGiverId into g
                 where g.Count() == cnt
                 select g.Key ?? 0;
             break;
         case CompareType.NotEqual:
             q = from c in Db.Contributions2(dt, now, 0, false, false, true)
                 where fund == 0 || c.FundId == fund
                 where c.Amount > 0
                 group c by c.CreditGiverId into g
                 where g.Count() != cnt
                 select g.Key ?? 0;
             break;
     }
     var tag = Db.PopulateTemporaryTag(q);
     Expression<Func<Person, bool>> pred = p => p.Tags.Any(t => t.Id == tag.Id);
     Expression expr = Expression.Invoke(pred, parm);
     return expr;
 }