0

I am trying to create a sorted list of lists. Where the outer "layer" is sorted by the field BU. and the inner layer is sorted by JobNum. I have this method, and variables.

public List<string> ListSorter<T>(IEnumerable<T> records)where T : 
IMapToCSVSource, new()

List<IEnumerable<T>> organizedRecords = new List<IEnumerable<T>>();
List<T> shortenedRecord = new List<T>();

Here I am trying to create a more concise object with only 3 specific fields instead of the possible 8 that comes with the records object. BU and JobNum are set within this method.

foreach (var record in records)
{
    string businessUnit = "research"; //This would vary each loop, not constant as displayed
    string JobNum = "test"       //this would vary each loop, not constant as displayed
    shortenedRecord.Add(new T()
    {
        Cost = record.Cost,
        BU = businessUnit,
        JobNum = jobNum
    });
}
shortenedRecord.OrderBy(o => o.BU).ThenBy(n=>n.JobNum);

here, to my knowledge, now has all the records shortened and ordered by BU, then JobNum.

Now I want to Split this ordered list into sections of BU specific records and add it to organizedRecords. Such that each element of organizedRecords is a specific BU. How would I do this?

For example, say shortenedRecord is a list of 30 elements, but there is only a total of 5 unique BU values. I would like to order and SPLIT the list into their 5 respective BU values, and add it to organizedRecords.

such that:

organizedRecords[0] should be a list of 'corporate' records

organizedRecords[1] should be a list of 'research' records

where corporate and research are BU values.

I tried to explain as best I could. Thank you for any suggestions.

1 Answers1

0

Thanks for suggestions, I toyed around a bit, and I found this to work as I needed it.

var query = shortenedRecord.GroupBy(p => p.BU).ToList();
foreach (var group in query)
{
    var ordered = group.OrderBy(x => x.JobNum);
    organizedRecords.Add(ordered);
}