2017-05-22 4 views
0

エンティティモデルとは異なる関係にあります。私は以下を探したい。LINQ/Lambdaを使用してリレーションナビゲーションキーからManyを選択するには?

表:カスタマー

[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; } 

} 

表:CompanyTable

[Table("CompanyTable")] 
public partial class CompanyTable 
{ 

    public CompanyTable() 
    { 
     CustomerTables = new HashSet<CustomerTable>(); 
     ProcessTables = new HashSet<ProcessTable>(); 
     DateRegistered = DateTime.Now; 
     PaymentDate = DateTime.Now; 

    } 

    [Key] 
    public int Id { get; set; } 

    [Required] 
    [StringLength(50)] 
    public string Name { get; set; } 
    public bool IsActive { get; set; } 
    public bool IsDeleted { get; set; } 
    public virtual ICollection<CustomerTable> CustomerTables { get; set; } 
    public virtual ICollection<ProcessTable> ProcessTables { get; set; } 

} 

表:プロセス

[Table("ProcessTable")] 
public partial class ProcessTable 
{ 
    [Key] 
    public int Id { get; set; } 

    [ForeignKey("CustomerTable")] 
    [Required] 
    public int CustomerId { get; set; } 


    [Required] 
    public DateTime DateCreated { get; set; } 


    [ForeignKey("CompanyTable")] 
    [Required] 
    public int Company { get; set; } 


    [Required] 
    public int TotalPayment { get; set; } 
    public int PaymentReceived { get; set; }// Store time of create process,1st time only 

    public virtual CompanyTable CompanyTable { get; set; } 
    public virtual CustomerTable CustomerTable { get; set; } 
    public virtual ICollection<ProcessPaymentMapping> 
    ProcessPaymentMappingList { get; set; } 
} 

タブル:(支払い履歴の)ProcessPaymentMapping

[Table("ProcessPaymentMapping")] 
public partial class ProcessPaymentMapping 
{ 
    public ProcessPaymentMapping() 
    { 
     CreatedOn = DateTime.Now; 
     isDeleted = false; 
    } 

    [Key] 
    public int Id { get; set; } 

    [ForeignKey("ProcessTable")] 
    public int ProcessId { get; set; } 

    [Required] 
    public decimal PaymentReceived { get; set; } 

    public string Remarks { get; set; } 

    public DateTime CreatedOn { get; set; } 

    public bool isDeleted { get; set; } 

    public virtual ProcessTable ProcessTable { get; set; } 
} 

私は、プロセスのための支払い&総支払残りを顧客賢明なレコードを表示します。

  • 会社には新しい顧客を作成する権利があります。 ( の合計支払いと顧客からの最初の支払いを保存している時に、 初期値をProcessPaymentMappingテーブルに保存する)

  • 1つの会社には複数のプロセスがあります。

  • 1つのプロセスに複数のProcessPaymentMappingレコードがあります。
  • ある顧客企業、
  • ProcessPaymentMappingテーブルによって登録された1つのまたは多くのプロセスが日付
  • で実行中のプロセスのために支払われるたびに顧客を支払い履歴を記憶しています。

プロセステーブルは、私は以下のように顧客賢明な支払いを表示したい会社

によってregisterd新しいプロセスの時のみ合計金額値を記憶しています。

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 

賢明に賢明です。 エンティティでグループを使用する方法..?

答えて

0

あなたもインデックスは、その後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 }) 
          .Select((x,i) => new { index = i, customerName = x.customerName, processId = x.processId, totalPayment = x.totalPayment, paidAmount = x.paidAmount, remainingAmount = x.remainingAmount}) 
          .ToList(); 

を使用したい場合は、この

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

を試してみてください。 3つの量をどのように扱うかは正確には分かりません。推測しました

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

実際には、ProcessPaymentMappingテーブルにgroupbyする必要があります。異なる日付で複数のprocessidと複数の入金が行われます。私たちはそれらを合計して、残っていることを知るためにProcess TableのTotal paymentを差し引く必要があります。 –

+0

GroupBy()を追加するコードを更新しました。 – jdweng

関連する問題