【问题标题】:DataTable Loop Within a Loop C#循环内的 DataTable 循环 C#
【发布时间】:2012-06-21 21:48:16
【问题描述】:

我有一个很好的数据集循环工作,但我需要根据父循环中的 ID 运行另一个循环。

我在一个单独的类中设置了一个通用列表,但我完全不知道如何实际调用它。我用谷歌搜索了它,但找不到我理解的例子。

编辑:

列出代码...

public class BinList
{
    public static List<Bin> GetById(int binOrderSiteID)
    {
        List<Bin> bins = new List<Bin>();

        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;

        using (conn = new SqlConnection(myConnectionHere))
        {
            comm = new SqlCommand("dbo.sl_BinsBySite", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@binOrderSiteID", SqlDbType.Int));
            comm.Parameters["@binOrderSiteID"].Value = binOrderSiteID;

            try
            {
                conn.Open();
                reader = comm.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Bin b = new Bin();
                        b.BinQty = reader["binQty"].ToString();
                        b.BinType = reader["binType"].ToString();
                        b.BinWasteGroupName = reader["binWasteGroupName"].ToString();
                        b.BinCollectionFrequencyType = reader["binCollectionFrequencyType"].ToString();
                        b.BinDeliveryStartDate = reader["binDeliveryStartDate"].ToString();
                        b.BinEmptyCharge = reader["binEmptyCharge"].ToString();
                        b.BinRentalCharge = reader["binRentalCharge"].ToString();
                        b.BinDutyOfCareCharge = reader["binDutyOfCareCharge"].ToString();
                        b.BinDeliveryCharge = reader["binDeliveryCharge"].ToString();
                        bins.Add(b);
                    }
                }
            }

            finally
            {
                conn.Close();
            }
        }

        return bins;
    }
}

这是每个字段的存储库

public class Bin
    {
        public string BinQty { get; set; }
        public string BinType { get; set; }
        public string BinWasteGroupName { get; set; }
        public string BinCollectionFrequencyType { get; set; }
        public string BinDeliveryStartDate { get; set; }
        public string BinEmptyCharge { get; set; }
        public string BinRentalCharge { get; set; }
        public string BinDutyOfCareCharge { get; set; }
        public string BinDeliveryCharge { get; set; }
    }

调用循环的代码

public class PDFCreator
{
    public static int BinOrderID { get; set; }

    private int binOrderID = 0;

    public PDFCreator(int intBinOrderID)
    {

    //Lots of code here

    //Data conenection/datatable code here

    foreach (DataRow row in dt.Rows)
            {
                //lots of code here

                //dont know how to connect up or call the List

                something?? = BinList.GetById(Convert.ToInt32(row["binOrderSiteID"].ToString()));

                foreach (//somnething here)
            }   
    }
}

抱歉,我最初没有添加我的代码。我不想展示它,因为我认为它是裤子。

有什么想法吗?

干杯, 麻木

【问题讨论】:

  • 请提供您的代码。不看就很难提出任何建议。
  • @fenix2222 抱歉,我问的是一般性问题。
  • 我也不好意思展示我的代码,因为我认为这是垃圾

标签: asp.net list datatable


【解决方案1】:

调用 BinList GetById

var binList = BinList.GetById((int)row["binOrderSiteID"]);

foreach (var bin in binList)
{  
    // do what you need to do with bin variable
}

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 2016-12-31
    • 2012-12-22
    • 2014-06-18
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多