2016-09-26 2 views
1

に詳細オブジェクトを投写​​私は.CSVを持っているデータには、次のようになります。C#のLINQ:要約オブジェクト

Account,Debit,Credit 

1,10.00,5.00 

1,10.00,10.00 

2,5.00,5.00 

2,10.00,10.00 

このデータはIEnumerable<Source>に移入されます。定義は以下の通りです:

public class Detail { 

    public string Account {get; set;} 
    public decimal Debit {get; set;} 
    public decimal Credit {get; set;} 

} 

私は合計でアカウントごとに借方と貸方の合計ですまとめオブジェクトには、この「詳細」オブジェクトを統合し、プロジェクトしようとしています。

public class Summary { 

    public string Account {get; set;} 
    public decimal Total {get; set;} 

} 

私が撮影しています最終結果ではなく、アカウント/デビット/クレジットごとに複数の行で、私は要約投影を持っているように、すべての借方と貸方は、アカウントごとにロールアップしたアカウントの明確なリストであります。

Account,Debit,Credit 


1,5.00,0.00 

2, 0.00, 0.00 

のはdetailrecordsはタイプIEnumerable<Detail>の人口の集まりであるとしましょう。

var results = detailrecords 
     .GroupBy(x => x.Account) 
     .Select(c => new Summary() { 
      Account = ?? I can't figure out how to access my detail properties here 
      Total = ?? I am not sure how to perform arithmetic without access to detail properties 
     }).ToList(); 

答えて

0

何を探してるんですが、このようなものである:それをやった

var results = detailRecords 
     .GroupBy(x => x.Account) 
     .Select(c => new Summary 
     { 
      Account = c.Key, 
      Total = c.Sum(y => y.Credit + y.Debit) 
     }).ToList(); 
+0

、ありがとうございました! –

+0

@MikeM Awesome! –