1👍
✅
You could change your linq like below:
public JsonResult OnGetRevenueData()
{
var year = _context.year.Select(a => a.Month).ToList();
var Revenue = (from t in _context.Revenue
where year.Contains(t.Month)
group t by new { Month = t.Month} into g
select new
{
Amount = g.Sum(a => a.Amount),
Month = g.Key.Month
}).ToList();
var countRevenue = Revenue.OrderBy(a => DateTime.ParseExact(a.Month, "MMMyy", CultureInfo.InvariantCulture).Month)
.Select(a=>a.Amount)
.ToList();
var countPayroll = (from t in _context.payroll
where year.Contains(t.Month)
group t by new { Month = t.Month } into g
select new {
Amount = g.Sum(a => a.Amount),
Month = g.Key.Month
}).ToList();
var countPayable = (from t in _context.payable
where year.Contains(t.Month)
group t by new { Month = t.Month } into g
select new
{
Amount = g.Sum(a => a.Amount),
Month = g.Key.Month
}).ToList();
//change here......
var leftOuterJoin = from a in countPayable
join b in countPayroll on a.Month equals b.Month into temp
from count in temp.DefaultIfEmpty()
select new
{
Month = a.Month,
Amount = a.Amount
};
var rightOuterJoin =
from b in countPayroll
join a in countPayable on b.Month equals a.Month into temp
from count in temp.DefaultIfEmpty()
select new
{
Month = b.Month,
Amount = b.Amount
};
var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);
var Expense = (from t in fullOuterJoin
group t by new
{
Month = t.Month
} into g
select new
{
Amount = g.Sum(a => a.Amount),
Month = g.Key.Month
}
).ToList();
var countExpense = Expense.OrderBy(a => DateTime.ParseExact(a.Month, "MMMyy", CultureInfo.InvariantCulture).Month)
.Select(a => a.Amount)
.ToList();
var yearList = ((from y in _context.year
select y.Month
)
.AsEnumerable()
.OrderBy(s => DateTime.ParseExact(s, "MMMyy", CultureInfo.InvariantCulture).Month)
).ToArray();
return new JsonResult(new { revenue = countRevenue, expense = countExpense, month = yearList });
}
Result:
Bar Chart:
payable table:
payroll table:
Revenue table:
year table:
Source:stackexchange.com