如何在C#中迭代地分配给对象内的嵌套列表?

时间:2018-08-20 18:40:41

标签: c#

我被要求复制内部系统中使用的复杂xml结构,以反对从另一个系统中检索到的数据。不幸的是xml结构是临时的,我们对此没有规范。

我一直在C#类中绘制它的结构,以便可以为这些属性分配数据库中的值,并最终将其序列化为xml。

在将新列表项迭代添加到对象中的嵌套列表方面,我遇到了一些障碍,该对象已经被初始化为列表并正在循环通过。为了使事情变得更复杂,我需要使用迭代中的值来过滤用于实例化第二轮循环的数据集。

很抱歉解释不正确-可以改写...希望我编写的示例可以演示我正在尝试做的事情:

using System;
using System.Collections.Generic;
using System.Data;

public class TransactionModel
{

    public int Id { get; set; }
    public string Description { get; set; }
    public DateTime SysDate { get; set; }
    public List<TransactionItemModel> Trade { get; set; } = new List<TransactionItemModel>();

}

public class TransactionItemModel
{

        public int ItemId { get; set; }
        public int TransactionId { get; set; }
        public string ItemDescription { get; set; }
        public decimal ItemNetAmount { get; set; }

    }

public class Program 
{
    public void Main()
    {
        DataTable tranResultSet = MethodToReturnResultsFromTranQuery();
        DataTable itemResultSet = MethodToReturnResultsFromItemQuery();

        var transactions = new List<TransactionModel>();
        foreach (DataRow tran in tranResultSet.Rows)
        {

            transactions.Add(

                new TransactionModel() {
                    Id = (dynamic)tran["Id"],
                    Description = (dynamic)tran["Description"],
                    SysDate = (dynamic)tran["SysDate"],
                    //Trade = <Stuck Here>
                    // Need to iterate through itemResultSet, adding to TransactionModel.Trade
                    // where item["TransactionId"] = tran["Id"]

                }

            );

        }

    }
}

1 个答案:

答案 0 :(得分:1)

此方法最初不会设置Trade集合,而是在您遍历Items时填充它。可能会添加很多优化,但这可能会让您入门。

public class TransactionModel
{
    public int Id { get; set; }
    public string Description { get; set; }
    public DateTime? SysDate { get; set; }
    public List<TransactionItemModel> Trade { get; set; }

    public TransactionModel(DataRow row)
    {
        if(row == null)
            throw new ArgumentNullException(nameof(row));

        Id = row.Field<int>("Id");
        Description = row.Field<string>("Description");
        SysDate = row.Field<DateTime?>("SysDate");
        Trade = new List<TransactionItemModel>();
    }
}

public class TransactionItemModel
{
    public int ItemId { get; set; }
    public int TransactionId { get; set; }
    public string ItemDescription { get; set; }
    public decimal? ItemNetAmount { get; set; }

    public TransactionItemModel(DataRow row)
    {
        if(row == null)
            throw new ArgumentNullException(nameof(row));

        ItemId = row.Field<int>("Id");
        TransactionId = row.Field<int>("TransactionId");
        ItemDescription = row.Field<string>("ItemDescription");
        ItemNetAmount = row.Field<decimal?>("ItemNetAmount");
    }
}

public static class Program
{
    private static void Main()
    {
        DataTable tranResultSet = MethodToReturnResultsFromTranQuery();
        DataTable itemResultSet = MethodToReturnResultsFromItemQuery();

        var transactions = tranResultSet.AsEnumerable()
                                        .Select(r => new TransactionModel(r));

        foreach(TransactionModel transaction in transactions)
        {
            var items = itemResultSet.AsEnumerable()
                                     .Where(r => r.Field<int>("TransactionId") == transaction.Id)
                                     .Select(r => new TransactionItemModel(r));

            transaction.Trade.AddRange(items);
        }
    }
}

根据当前的TransactionId查询您的ItemResultSet而不是先将它们全部抢占起来可能是理想的选择。您可以实现DataReader或使用Dapper。