hoakey
hoakey

Reputation: 998

How do I combine mulitple single columns Lists into a multi column single List?

I have the following class:

    public class AuditBatch
{
    public List<string> Tables { get; set; }
    public List<long> TotalRecords { get; set; }
    public List<int> Index { get; set; }

This is then called by another class, and values are added to these lists:

auditBatch.Tables.Add(extractQuery.Category);
auditBatch.TotalRecords.Add(rowsInFile + totalRowsInFile);
auditBatch.Index.Add(fileIndex);  

I then want to be able add these Lists to a List that takes each of these lists as a column so it would look something like this:

Tables | TotalRecords | FieldIndex
John      333             1
Paul      200             4
Ringo     890             1

At a later date I might be interested grouping by FieldIndex and taking the highest TotalRecords Row.

I've followed lots of examples but can't seem to find anything that works.

Upvotes: 0

Views: 39

Answers (1)

DLCross
DLCross

Reputation: 704

It appears that you are storing data in three parallel lists. The only link between the items is the list index, which can be accessed using LINQ, but this solution is far more awkward than it needs to be.

12seconds is right: you should restructure your type so that you are adding related information in a single record. Like this:

public class AuditRow
{
    public string Table { get; set; }
    public long TotalRecords { get; set; }
    public int Index { get; set; }
}

Then, create a list of rows and populate it one at a time:

        List<AuditRow> auditBatch = new List<AuditRow>();

        AuditRow row = new AuditRow();
        row.Table = "John";
        row.TotalRecords = 333;
        row.Index = 1;

        auditBatch.Add(row);

        //etc. for each row.

Upvotes: 1

Related Questions