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();
}
Este é o meu código. Desejo carregar dados por meio de workerId e somar sua coluna específica, mas enfrento este erro ... System.NotSupportedException: 'Não é possível criar um valor constante do tipo' Tipo anônimo '. Apenas tipos primitivos ou tipos de enumeração são suportados neste contexto. '
A primeira coisa que você pode tentar:
var worker = from w in db.Workers
.Where(w => totalDistributed.Select(td => td.WorkerId).Contains(w.WorkerId))
.Select(w => new { w.WorkerId,
w.WorkerName,
w.Address,
w.Contact,
w.ReferenceName,
w.RefereceContact})
.ToList()
join d in totalDistributed on w.WorkerId equals d.WorkerId
....
Além disso, eu diria que você pode tentar executar toda essa consulta no lado do banco de dados. Tente remover AsEnumerable
e ToList
partir workerPayment
, collection
e totalDistributed
em seu código original.
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras