Question

Ok so I get data from mySql and it comes back as a ReadOnlyCollection<Tuple<DbAudit, DbAuditItem>>. Now I have to map from this to my Audit and AuditItem classes. The Audit part is working like so:

return audits.Select((AuditMapper.Map)).ToReadOnlyCollection();

and in the auditmapper this happens.

public static Audit Map(Tuple<DbAudit, DbAuditItem> source)
{
  if (source == null)
    return null;

  return new Audit(
    null,
    (AuditKind) source.Item1.AuditKindId,
    source.Item1.DateCreated,
    null,
    source.Item1.ContainerItemId,
    source.Item1.UserId,
    Item2.Select(AuditItemMapper.Map).ToReadOnlyCollection()
);
}

but the Item2.Select... line isn't working and i'm not sure how else to write it. Any ideas?

EDIT: First the Item2 line is supposed to be source.Item2.Select.

I've also realized my logic isn't making sense because each Item2 is only one auditItem and i'm trying to add it to a collection. Audit item looks like this:

public Audit(string applicationToken, AuditKind auditKind, DateTime dateCreated, string containerName, string containerItemId, int userId, ICollection<AuditItem> auditItems)
    {
        m_applicationToken = applicationToken;
        m_auditKind = auditKind;
        m_dateCreated = dateCreated;
        m_containerName = containerName;
        m_containerItemId = containerItemId;
        m_userId = userId;
        m_auditItems = auditItems;
    }

and the auditItem is:

public AuditItem(string name, string data, string oldData)
    {
        m_name = name;
        m_data = data;
        m_oldData = oldData;
    }

But I see that the way I have is isn't going to work. I need somehow to take each distinct Item1 of the tuple and put each corresponding Item2 into a collection that will go into the audit item...

Was it helpful?

Solution

I ended up doing it in a roundabout way like so:

Audit audit = null;
            List<AuditItem> auditItemsList = new List<AuditItem>();

            List<Audit> totalAudits = new List<Audit>();

            ulong thisId = 0;

            foreach (Tuple<DbAudit, DbAuditItem> tuple in audits)
            {
                if (tuple.Item1.Id == thisId)
                {
                    auditItemsList.Add(AuditItemMapper.Map(tuple.Item2));
                }
                else
                {
                    if (thisId != 0 && audit != null)
                    {
                        totalAudits.Add(new Audit(audit.ApplicationToken, audit.AuditKind, audit.DateCreated, audit.ContainerName, audit.ContainerItemId, audit.UserId, auditItemsList));
                        auditItemsList.Clear();
                    }
                    thisId = tuple.Item1.Id;
                    audit = (AuditMapper.Map(tuple.Item1));
                    auditItemsList.Add(AuditItemMapper.Map(tuple.Item2));
                }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top