2012-06-21 79 views
-2

我有一個很好的數據集循環工作,但我需要運行基於父循環中的ID另一個循環。數據表循環內循環C#

我在一個單獨的類中設置了一個通用列表,但我完全難以確定如何實際調用它。我谷歌搜索,但無法找到我明白的例子。

編輯:

代碼列表...

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

對不起我沒有添加我的代碼原來。我不想展示它,因爲我認爲它是褲子。

任何想法?

乾杯, 麻木

+2

請提供你的代碼。沒有看它就很難提出任何建議。 – fenix2222

+0

@ fenix2222對不起,我以爲我在問一個普遍的問題。 – ComfortablyNumb

+0

我也很尷尬,顯示我的代碼因爲我認爲這是垃圾 – ComfortablyNumb

回答

0

要調用BinList GetById

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

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

謝謝 - 它工作正常 - 所以它不是太遠! – ComfortablyNumb