繁体   English   中英

如何通过两个表之间的 ID 连接和分组并通过 ID 获取字段总和 - Linq C#?

[英]how can Join and Group by id between Two Tables and Get Sum of Fields - Linq C# through Id?

  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();

    }

这是我的代码,我想通过 workerId 加载数据并对它们的特定列求和,但我遇到了这个错误..System.NotSupportedException:'无法创建'匿名类型'类型的常量值。 在此上下文中仅支持原始类型或枚举类型。

  1. 我的代码错了吗?
  2. 如果它是错误的,哪个正确或更好的方法呢?

您可以尝试的第一件事:

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
....

另外我想说您可以尝试在数据库端执行整个查询。 尝试从您的原始代码中的workerPaymentcollectiontotalDistributed中删除AsEnumerableToList

  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();
    }

//通过使用此代码在drid视图中正确加载数据,但1个数据重复3次问题出在哪里?

您不能将 LINQ 查询与本地对象列表连接起来,因为它无法转换为 SQL。

因此,请使用此处提供的解决方案。

无法创建类型的常量值 此上下文仅支持原始类型或枚举类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM