tags:

views:

1249

answers:

2

Here's my Class that I'm grouping:

public class RefundTran : DataObjectBase<RefundTran>
    {
        public string ARTranID { get; set; }
        public decimal Amount { get; set; }
        public string ARTranTypeCode { get; set; }
        public int CheckNumber { get; set; }
        public int CustID { get; set; }
        public string PaymentTypeCode { get; set; }
        public string PostedFlag { get; set; }
        public decimal TaxAmount { get; set; }
        public string TranDate { get; set; }
        public string RefNumber { get; set; }
        public decimal Balance { get; set; }
    }

Here's the List<>:

    List<RefundTran> trans = batchRefundToProcess.RefundCustomer.ARTransactions;

And here's the Linq query I've got so far:

var TransGroupedByType =
                    from t in trans
                    group t by t.PaymentTypeCode into g
                    select new { CustID = g.First<RefundTran>().CustID, PaymentTypeCode = g.Key, TotalBalance = g.Sum (p=> p.Balance) };

Basically, what I want is a group of transactions, keyed on both paymentTypeCode and TotalBalance, containing an array of RefundTran objects for that payment type code.

What am I missing?

Thanks in advance!

A: 

group t by t.PaymentTypeCode

consider changing that to:

group t by new { t.PaymentTypeCode, t.TotalBalance }

it you want to group by both properties. You'll then be able to access g.Key.PaymentTypeCode, g.Key.TotalBalance

David Hedlund
How do I get TotalBalance without using an aggregate? Note that TotalBalance is a computed field in the Linq query itself!
Chris McCall
you're right, i missed that part before, for pretty much the same reason that bruno conde comments above: the request doesn't seem to make much sense. You say you want it keyed on TotalBalance, yet TotalBalance is an aggregate of the list. In what sense, then, do you want TotalBalance to be a part of the key? And if you don't, what is it about your result you're not happy with?
David Hedlund
group- paymenttype code, total for each paymenttypeeach item in group- a transaction, representing detail for the transaction groupI need the total so that I only issue a refund in a given type once for each set of transactions. If the request doesn't seem to make sense, why are you answering?
Chris McCall
+1  A: 

It seems you want an array of transactions too, you should add it to the anonymous type:

var TransGroupedByType = from t in trans
                         group t by t.PaymentTypeCode into g
                         select new { 
                            CustID = g.First().CustID, 
                            PaymentTypeCode = g.Key, 
                            TotalBalance = g.Sum(p => p.Balance),
                            TransactionList = g.ToArray()
                         };
Mehrdad Afshari
Voting this one up as it is close to what I came up with and seems closest to what the asker may actually want. Chris: Please let us know what this result set has that you don't like!
Ryan Versaw
I like it so far, going to have to play around with it in code for a little bit. I don't want to discourage new answers so I'll wait for it to "settle" for a couple of hours :)
Chris McCall