//Use this when implement delete feature public void DeleteContributor(int id) { using (var context = new ContributionDataContext(_connectionString)) { context.ExecuteCommand("DELETE FROM Contributors WHERE Id = {0}", id); //can do with context.Delete but that requires 2 database hits } }
private void SetSimchaTotals(Simcha simcha) { using (var context = new ContributionDataContext(_connectionString)) { simcha.Total = context.Contributions.Any(c => c.SimchaId == simcha.Id) ? context.Contributions.Where(c => c.SimchaId == simcha.Id).Select(c => c.Amount).Sum() : 0; simcha.ContributorAmount = context.Contributions.Any(c => c.SimchaId == simcha.Id) ? context.Contributions.Count(c => c.SimchaId == simcha.Id) : 0; } }
public IEnumerable <Contribution> GetContributionsById(int contribId) { List <Contribution> contributions = new List <Contribution>(); using (var context = new ContributionDataContext(_connectionString)) { contributions = context.Contributions.Any(c => c.ContributorId == contribId) ? context.Contributions.Where(c => c.ContributorId == contribId).ToList() : null; } return(contributions); }
public decimal GetContributorBalance(int contributorId) { decimal contributions = 0; decimal deposits = 0; using (var context = new DepositDataContext(_connectionString)) { deposits = context.Deposits.Where(d => d.ContributorId == contributorId).Select(d => d.Amount).Sum(); } using (var context = new ContributionDataContext(_connectionString)) { contributions = context.Contributions.Any(c => c.ContributorId == contributorId) ? context.Contributions.Where(c => c.ContributorId == contributorId).Select(c => c.Amount).Sum() : 0; } return(deposits - contributions); }
public void UpdateSimchaContributions(int simchaId, List <ContributionInclusion> contributors) { using (var context = new ContributionDataContext(_connectionString)) { context.ExecuteCommand("DELETE FROM Contributions WHERE SimchaId = {0}", simchaId); //can do with context.Delete but that requires 2 database hits foreach (var c in contributors) { context.Contributions.InsertOnSubmit(new Contribution { SimchaId = simchaId, ContributorId = c.ContributorId, Amount = c.Amount }); } } }
public decimal GetTotal() { //can make code prettier by using GetContributorBalance //but that's two database hits per contributor - this is two hits total decimal contributions = 0; decimal deposits = 0; using (var context = new ContributionDataContext(_connectionString)) { contributions = context.Contributions.Any() ? context.Contributions.Select(c => c.Amount).Sum() : 0; } using (var context = new DepositDataContext(_connectionString)) { deposits = context.Deposits.Select(d => d.Amount).Sum(); } return(deposits - contributions); }
public IEnumerable <SimchaContributor> GetSimchaContributors(int simchaId) { List <SimchaContributor> simchaContributors = new List <SimchaContributor>(); using (var context = new ContributionDataContext(_connectionString)) { var allContributors = GetContributors(); var currentContributions = context.Contributions.Where(c => c.SimchaId == simchaId); foreach (Contributor c in allContributors) { simchaContributors.Add(new SimchaContributor { FirstName = c.FirstName, LastName = c.LastName, AlwaysInclude = c.AlwaysInclude, ContributorId = c.Id, Balance = GetContributorBalance(c.Id), Amount = currentContributions.FirstOrDefault(contribution => contribution.ContributorId == c.Id) != null ? currentContributions.FirstOrDefault(contribution => contribution.ContributorId == c.Id).Amount : 0 }); } return(simchaContributors.AsEnumerable()); } }