0

I have two table That i want to combine to generate a report

User List

Transaction

I use this code to join the table

db.tblProcessorLists.GroupJoin(
                 db.tbltransactions,
                 a => a.UserID,
                 b => b.UserID,
                 (x, y) => new
                 {
                     Processor = x,
                     Data = y.ToList()
                 }
         )
         .Select(a => new Report
         {
             Processor = a.Processor,
             TotalTrans = a.Data
         }).ToList();

and this the output

enter image description here

I want to insert the data in my report class.

enter image description here

is this possible and how?

EDIT I already tried this but its not working

db.tblProcessorLists.GroupJoin(
                 db.tbltransactions,
                 a => a.UserID,
                 b => b.UserID,
                 (x, y) => new
                 {
                     Processor = x,
                     Data = y.ToList()
                 }
         )
         .Select(a => new Report
         {
             Processor = a.Processor,
             SummaryTransaction = a.Data.Select(x=>new Summaryreport{ month=x.month, year=x.year, total=x.totaltransaction})
         }).ToList();
2
  • @TDK I already add what i try
    – Vic
    Feb 11, 2019 at 10:04
  • You may get more answers tagging your question with C# and LINQ rather than WPF (I don't see any WPF in your question).
    – redcurry
    Feb 11, 2019 at 15:41

1 Answer 1

2

Although you didn't specify, I think what you want is to sum all Data of one month. So instead of:

Processor Month   Value
    1    2018-1     2
    1    2018-1     3  => sum all 2018-1 values of processor 1: 2+3=5
    1    2018-2     4
    1    2018-2     5  => sum all 2018-2 values of processor 1: 4+5=9
    2    2018-1     1
    2    2018-1     2  => sum all 2018-1 values of processor 2: 1+2=3

All you have to do is change the ResultSelector in your GroupJoin, such that it groups all data into groups with same [year, month]. The result is [year, month, sum] of all values with this year and month]

See one of the overloads of Enumerable.GroupBy

var result = db.tblProcessorLists.GroupJoin(  // GroupJoin processors and transactions
    db.tbltransactions,
    processor => processor.UserID,       // from every processor take the UserId
    transaction => transaction.UserID,   // from every transaction take the UserId

    // ResultSelector: use eacht processor with its zero or more matching transactions
    // to make one new object
    (processor, transactionsOfThisProcessor) => new
    {
        Processor = processor,

        // group all transactionsOfThisProcessor into groups with same [year, month]
        // and sum the values of all transactions in each group
        TransactionTotals = transactionsOfThisProcessor
            .GroupBy(transaction => new 
            {
                Year = transaction.Year,       // KeySelector: make groups with 
                Month = transaction.Month      // same [year, month]
            },

            // element selector: Transaction.Value
            transaction => transaction.Value,

            // ResultSelector: key = [year, month],
            // elements are all values of this [year, month] group
            // sum all values
            (key, valuesWithThisYearMonth) => new
            {
                Year = key.Year,
                Month = key.Month,
                Total = valuesWithThisYearMonth.Sum(),
            }),
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.