如何在两个表之间通过id进行连接和分组,并获得字段的总和——Linq C#through id?
这是我的代码我想通过workerId加载数据并对其特定列求和,但我遇到了以下错误..System.NotSupportedException:'无法创建“Anonymous type”类型的常量值'。在此上下文中仅支持基元类型或枚举类型。'如何在两个表之间通过id进行连接和分组,并获得字段的总和——Linq C#through id?,c#,C#,这是我的代码我想通过workerId加载数据并对其特定列求和,但我遇到了以下错误..System.NotSupportedException:'无法创建“Anonymous type”类型的常量值'。在此上下文中仅支持基元类型或枚举类型。' 是我的代码错了吗 如果是错误的,哪种方法正确或更好 不能将LINQ查询与本地对象列表联接,因为它不能转换为SQL 因此,请使用此处提供的解决方案 您可以尝试的第一件事: private class DataBindingProjection {
不能将LINQ查询与本地对象列表联接,因为它不能转换为SQL 因此,请使用此处提供的解决方案
您可以尝试的第一件事:
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();
}
我还想说,您可以尝试在db端执行整个查询。尝试从原始代码中的workerPayment
、collection
和totalDistributed
中删除aEnumerable
和ToList
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
....
//通过在drid视图中正确使用此代码数据加载,但1个数据重复了3次,问题出在哪里?在查询之后添加
.ToList()
我不知道问题出在哪里?也许我已经补充说了这不是一个答案(只是链接到另一个答案),这样做是任何性能POV的可怕建议
private class DataBindingProjection
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string Address { get; set; }
public string Contact { get; set; }
public string ReferenceName { get; set; }
public string ReferenceContact { get; set; }
public int TotalSaleOfPiece { get; set; }
public int TotalReturn { get; set; }
public int TotalDue { get; set; }
}
private void CustomerListForm_Load(object sender, EventArgs e)
{
var customer = from c in db.Customers
join s in db.Sales on c.CustomerId equals s.CustomerId
group c by new { c.CustomerId } into y
from customer1 in db.Customers.DefaultIfEmpty().ToList()
select new DataBindingProjection
{
CustomerId = customer1.CustomerId,
CustomerName = customer1.ShopName,
Address = customer1.Address,
Contact = customer1.Contact,
ReferenceName = customer1.ReferenceName,
ReferenceContact = customer1.RefereceContact,
TotalSaleOfPiece = customer1.Sales.Sum(m => (int?)m.Piece) ?? 0,
TotalReturn = customer1.Sales.Sum(m => (int?)m.ReturnPiece) ?? 0,
TotalDue = customer1.Sales.Sum(m => (int?)m.Due) ?? 0,
};
customerDataGridView.DataSource = customer.ToList();
}