0

I am a little new to C#, sorry if the question is rudimentary.

I have an anonymous list as result of the following:

var Totals = model.Payments.GroupBy(pm => pm.PaymentType)
             .Select(t => new
               {
                    PaymentType = t.Key,
                    Amount = t.Sum(pm => pm.Amount)
               });

Returns Totals as anonymous list with two entries.

Cash 10000.00

EFT 8000.00

Now I need to add this list to a typed list in my viewmodel. The typed list looks like this

public class PaymentExportViewModel
{
    public List<PaymentReportViewModel> Payments { get; set; }
    public List<PaymentSubTotals> Subs { get; set; }
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public String VenueName { get; set;}
    public PaymentExportViewModel()
    {
        Payments = new List<PaymentReportViewModel>();
        Subs = new List<PaymentSubTotals>();
    }
}

public class PaymentSubTotals
{
    public string Type { get; set; }
    public decimal Amount { get; set; }
}

So since I need to "convert" to typed I do this

PaymentSubTotals subs = new PaymentSubTotals();
foreach (var t in Totals)
{
    subs.Type = t.PaymentType;
    subs.Amount = t.Amount;
    model.Subs.Add(subs); 
}

The result is that model.subs now contains 2 entries but both are the same (last entry in my loop) EFT 8000.00 EFT 8000.00

What am I missing in the model.Subs.Add ? Or somewhere else ?

1
  • 2
    why project it to an anonymous type to begin with when you can just project it to a strongly typed class Commented Feb 8, 2017 at 15:59

3 Answers 3

2

Move declation of subs inside of foreach loop:

foreach (var t in Totals)
{
    PaymentSubTotals subs = new PaymentSubTotals();
    subs.Type = t.PaymentType;
    subs.Amount = t.Amount;
    model.Subs.Add(subs); 
}

In your code you change the same sub object every time, so you do have only the data of the last t variable, repeated.

Sign up to request clarification or add additional context in comments.

3 Comments

That is because in C#, when you add an item to a list it is passed by reference, not a copy, right? That's why when you modify subs, it modifies the item in the list?
@GustavoZanardini yes, it's a reference type - class, so in the list are just n references to the same object.
Of course, I feel a little silly now. Hopefully it can help someone else.
2

Why don't you Select into your PaymentSubTotals instead.

var Totals = model.Payments.GroupBy(pm => pm.PaymentType)
.Select(t => new PaymentSubTotals
{
 Type = t.Key,
 Amount = t.Sum(pm => pm.Amount)
});

Comments

1

I would recommend the following method to add a new PaymentSubTotals object on each loop.

foreach (var t in Totals)
{
    model.Subs.Add(new PaymentSubTotals {
        Type = t.PaymentType;
        Amount = t.Amount;
    }); 
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.