0

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

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

在 auditmapper 中会发生这种情况。

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 放入一个集合中,该集合将进入审计项......

4

1 回答 1

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));
                }
            }
于 2013-06-28T17:48:38.980 回答