从元组映射到ICollection

时间:2013-06-28 00:32:47

标签: c# mysql tuples icollection

好的,我从mySql获取数据,然后以ReadOnlyCollection <Tuple<DbAudit, DbAuditItem>>的形式返回。现在我必须从这里映射到我的AuditAuditItem类。 Audit部分的工作原理如下:

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

并且在审计映射器中会发生这种情况。

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()
);
}

但是Item2.Select ...行无效,我不知道怎么写它。有什么想法吗?

编辑: 首先,Item2行应该是source.Item2.Select。

我也意识到我的逻辑没有意义,因为每个Item2只是一个auditItem,而我正在尝试将它添加到集合中。审核项目如下所示:

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;
    }

,auditItem是:

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

但是我看到我的方式不会起作用。我需要以某种方式获取元组的每个不同的Item1,并将每个相应的Item2放入一个将进入审计项目的集合中......

1 个答案:

答案 0 :(得分:0)

我最终以迂回的方式这样做:

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));
                }
            }