linq - MVC C# Creating CSV File with a 0 balance filter -
i not sure how name title hope got close. code below, , prints out way should. having troubles is, row has unpaid balance of 0 or less needs not printed. math done in different scope (i believe) can't 'oh yea way don't print out unpaid balance if it's 0 or less. how create while/if/foreach statement print rows csv file when unpaid balance less or equal zero?
public ienumerable<rowviewmodel> getunpaidrr(int year, type[] types) { foreach (var type in types) type.fees = type.fees.tolist(); var transactions = _db.transactions.where(i => i.id.equals(status.success.id)); return _db.locations .include(i => i.counts) .where(i => i.owner.report.completed != null && i.owner.report.id == year && i.owner.primary == null) .select(location => new { id = location.owner.report.id, year = location.owner.report.year.name, counts = location.counts.select(i => new { i.id, i.count, }), firstname = location.owner.firstname, lastname = location.owner.lastname, paid = transactions.where(t => t.id == location.owner.report.id).select(n => new { n.amount }).tolist(), }) .asenumerable() // takes transactions match selected customer id .select(i => new rowviewmodel(types) { id = i.id.hasvalue ? i.id.value.toidstring() : string.empty, year = i.year, firstname = i.firstname, lastname = i.lastname, unpaidbalance = types.sum(j => i.counts.where(k => k.id == j.id).sum(k => k.count * j.fees.where(p => p.id == year).select(p => p.cost).singleordefault())) - i.paid.sum(j => j.amount),// math, takes amount charged, subtracts amount paid , produces balance. if 0 or less, don't want row printed. }); }
you calculate unpaidbalance
property in last linq clause:
.select(i => new rowviewmodel(types) { id = i.id.hasvalue ? i.id.value.toidstring() : string.empty, year = i.year, firstname = i.firstname, lastname = i.lastname, unpaidbalance = types.sum(j => i.counts.where(k => k.id == j.id).sum(k => k.count * j.fees.where(p => p.id == year).select(p => p.cost).singleordefault())) - i.paid.sum(j => j.amount) })
simply add .where()
clause after that:
.where(i => i.unpaidbalance <= 0)
if performance issue may able move calculation , filter earlier in overall sequence of events, if need filter results that's .where()
for.
(note: there confusion in how describe filter. in places want exclude values <= 0
, , in other places want exclusively include values <= 0
. whatever logic need, however, go in .where()
clause.)
Comments
Post a Comment