C# 如何使用LINQ/Lambda从关系导航键中获取selectmany?
我们与实体模型有着不同的关系。我想找到下面的答案 表:客户C# 如何使用LINQ/Lambda从关系导航键中获取selectmany?,c#,asp.net-mvc,entity-framework,linq,plinq,C#,Asp.net Mvc,Entity Framework,Linq,Plinq,我们与实体模型有着不同的关系。我想找到下面的答案 表:客户 [Table("CustomerTable")] public partial class CustomerTable { [Key] public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } [Required] [StringLength(20)]
[Table("CustomerTable")]
public partial class CustomerTable
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(20)]
public string Country { get; set; }
public virtual CompanyTable CompanyTable { get; set; }
public virtual CurrencyTable CurrencyTable { get; set; }
public virtual LoginTable LoginTable { get; set; }
public virtual ICollection<ProcessTable> ProcessTables { get; set; }
}
我想显示客户的记录,包括流程的剩余付款和总付款
- 公司有权创建新客户。(我们正在储存的时间 客户的总付款和首次付款,同时 将初始值存储到ProcessPaymentMapping表)
- 一个公司有多个流程
- 一个进程有多个ProcessPaymentMapping记录李>
- 一个客户有一个或多个由公司注册的流程
- ProcessPaymentMapping表存储了带有日期的付款历史记录
- 每次客户为运行流程付费时
我想显示如下客户付款
Index | Customer Name | Process ID | Total Amount | Paid Amount | Remaining Amount
1. | Jhon Michel | 544545 | 150 USD | 100 USD | 50 USD
2. | Arena Bosch | 544546 | 200 USD | 50 USD | 150 USD
像智者一样。
如何在实体中使用分组依据?试试这个
CompanyTable companyTable = new CompanyTable();
var results = (from ct in companyTable.CustomerTables
join pt in companyTable.ProcessTables on ct.Id equals pt.CustomerId
select new { customerName = ct.CompanyTable.Name, processId = pt.Id, totalPayment = pt.TotalPayment, paidAmount = pt.PaymentReceived, remainingAmount = pt.TotalPayment - pt.PaymentReceived }).ToList();
如果您还需要索引,请使用此
var results = (from ct in companyTable.CustomerTables
join pt in companyTable.ProcessTables on ct.Id equals pt.CustomerId
select new { customerName = ct.CompanyTable.Name, processId = pt.Id, totalPayment = pt.TotalPayment, paidAmount = pt.PaymentReceived, remainingAmount = pt.TotalPayment - pt.PaymentReceived })
.Select((x,i) => new { index = i, customerName = x.customerName, processId = x.processId, totalPayment = x.totalPayment, paidAmount = x.paidAmount, remainingAmount = x.remainingAmount})
.ToList();
添加一个GroupBy。不太清楚如何处理这三个金额。猜猜看
var results = (from ct in companyTable.CustomerTables
join pt in companyTable.ProcessTables on ct.Id equals pt.CustomerId
select new { customerName = ct.CompanyTable.Name, processId = pt.Id, totalPayment = pt.TotalPayment, paidAmount = pt.PaymentReceived, remainingAmount = pt.TotalPayment - pt.PaymentReceived })
.GroupBy(x => x.processId)\
.Select((x,i) => new { index = i, customerName = x.FirstOrDefault().customerName, processId = x.FirstOrDefault().processId,
totalPayment = x.Sum(y => y.totalPayment), paidAmount = x.Sum(y => y.paidAmount), remainingAmount = x.Sum(y => y.remainingAmount)})
.ToList();
实际上,在ProcessPaymentMapping表上需要groupby,将有多个processid条目和多个在不同日期收到的付款。。我们必须把它们加起来,然后在工艺表的总付款上加上差额,才能知道剩余金额。。
CompanyTable companyTable = new CompanyTable();
var results = (from ct in companyTable.CustomerTables
join pt in companyTable.ProcessTables on ct.Id equals pt.CustomerId
select new { customerName = ct.CompanyTable.Name, processId = pt.Id, totalPayment = pt.TotalPayment, paidAmount = pt.PaymentReceived, remainingAmount = pt.TotalPayment - pt.PaymentReceived }).ToList();
var results = (from ct in companyTable.CustomerTables
join pt in companyTable.ProcessTables on ct.Id equals pt.CustomerId
select new { customerName = ct.CompanyTable.Name, processId = pt.Id, totalPayment = pt.TotalPayment, paidAmount = pt.PaymentReceived, remainingAmount = pt.TotalPayment - pt.PaymentReceived })
.Select((x,i) => new { index = i, customerName = x.customerName, processId = x.processId, totalPayment = x.totalPayment, paidAmount = x.paidAmount, remainingAmount = x.remainingAmount})
.ToList();
var results = (from ct in companyTable.CustomerTables
join pt in companyTable.ProcessTables on ct.Id equals pt.CustomerId
select new { customerName = ct.CompanyTable.Name, processId = pt.Id, totalPayment = pt.TotalPayment, paidAmount = pt.PaymentReceived, remainingAmount = pt.TotalPayment - pt.PaymentReceived })
.GroupBy(x => x.processId)\
.Select((x,i) => new { index = i, customerName = x.FirstOrDefault().customerName, processId = x.FirstOrDefault().processId,
totalPayment = x.Sum(y => y.totalPayment), paidAmount = x.Sum(y => y.paidAmount), remainingAmount = x.Sum(y => y.remainingAmount)})
.ToList();