tags:

views:

242

answers:

1

I have a list of records with the following structure: (Simplified example!)

class Rate
{
    string Code;
    double InterestFrom;
    double InterestTo;
    double IncomeFrom;
    double IncomeTo;
    double Value;
}

And yes, I have a List<Rate> defined. I need to convert this list to the following simplified structure:

class RateList
{
   List<Code> code;
}
class Code
{
    string code;
    List<Interest> interest;
}
class Interest
{
    double InterestFrom;
    double InterestTo;
    List<Income> income;
}
class Income
{
    double IncomeFrom;
    double IncomeTo;
    double Value;
}

And I would like to do this with a single LINQ query. I can work out other solutions but the challenge is to do it within one LINQ statement. Basically, the top-level group is grouped by code. The next level by the values InterestFrom and InterestTo and the lowest level is a list of IncomeFrom, IncomeTo and Value.

So, what would be the LINQ statement for this?

+4  A: 

This is pretty huge, but here is what I have:

var rateList = new RateList
               {
                   code = (from r in rates
                           group r by r.Code into g
                           select new Code
                           {
                               code = g.Key,
                               interest = (from i in g
                                           group i by new {i.InterestFrom, i.InterestTo} into g2
                                           select new Interest
                                           {
                                               InterestFrom = g2.Key.InterestFrom,
                                               InterestTo = g2.Key.InterestTo,
                                               income = (from inc in g2
                                                         select new Income
                                                         {
                                                             IncomeFrom = inc.IncomeFrom,
                                                             IncomeTo = inc.IncomeTo,
                                                             Value = inc.Value
                                                         }).ToList()
                                           }).ToList()
                           }).ToList()
               };
Ryan Versaw
Huge is no problem. :-) Just want to know if it can be done in a single statement. (And then checking if it stays readable...)
Workshop Alex
This is the most concise way I can think to do it right now. The only good way I know of for you to shorten it would be to have some simple constructors on those classes. It wouldn't cut too much out, but it would help a little.
Ryan Versaw
And checked the code! It works! It's a monster statement but then again, I want to discourage the less experienced developers from modifying it. And to be honest, it's still very readable.
Workshop Alex
Shortening isn't really needed. Modifying the constructors isn't possible, though, since the classes I use are not created by me. Still, the final code is less than 30 lines and still readable, as long as you use a wide screen. :-) (Basically, I'm filling this structure to pass it onwards to a third-party module, which probably translates it back to a flat table again.)
Workshop Alex