private class DataBindingProjection
{
public int WorkerId { get; set; }
public string WorkerName { get; set; }
public string Address { get; set; }
public string Contact { get; set; }
public string ReferenceName { get; set; }
public string ReferenceContact { get; set; }
public int TotalDistribute { get; set; }
public int TotalCollection { get; set; }
public int TotalDeposit { get; set; }
public int TotalPayment { get; set; }
public int TotalPayable { get; set; }
}
private void WorkerLIstForm_Load(object sender, EventArgs e)
{
var totalDistributed = (db.Distributions.AsEnumerable().GroupBy(d => d.WorkerId)).Select(a => new
{
WorkerId = a.Key,
Amount = a.Sum(r => r.Piece)
}).ToList();
var collection = (db.Collections.AsEnumerable().GroupBy(d => d.WorkerId)).Select(a => new
{
WorkerId = a.Key,
Piece = a.Sum(r => r.Piece),
Deposit = a.Sum(r => r.PayableDeposit)
}).ToList();
var workerPayment = (db.WorkerPayments.AsEnumerable().GroupBy(d => d.WorkerId)).Select(a => new
{
WorkerId = a.Key,
Payment = a.Sum(r => r.Payment),
PayableAmount = a.Sum(r => r.PayableAmount)
}).ToList();
var worker = from w in db.Workers
join d in totalDistributed on w.WorkerId equals d.WorkerId
join c in collection on d.WorkerId equals c.WorkerId
join wp in workerPayment on w.WorkerId equals wp.WorkerId
select new DataBindingProjection
{
WorkerId = w.WorkerId,
WorkerName = w.WorkerName,
Address = w.Address,
Contact = w.Contact,
ReferenceName = w.ReferenceName,
ReferenceContact = w.RefereceContact,
TotalDistribute = d.Amount,
TotalCollection = c.Piece,
TotalDeposit = c.Deposit,
TotalPayment = wp.Payment,
TotalPayable = wp.PayableAmount
};
workerDataGridView.DataSource = worker.ToList();
}
This is my code I want to load data through workerId and sum their specific column but I face this error..System.NotSupportedException: 'Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.'
- Is my code is wrong?
- If it's wrong which the correct or better approach for it?