简体   繁体   English

C#foreach循环意外跳过一些行

[英]C# foreach loop skip some rows unexpectedly

It seems to be a severe and suspicious issue i have a simple for-each loop which iterates through a data table rows. 我有一个简单的for-each循环,它遍历数据表的行,这似乎是一个严重且可疑的问题。 My code was working fine for almost a year suddenly this issue appears a month ago. 我的代码运行正常将近一年,突然这个问题在一个月前出现。

I done text file based logging and observed that for-each loop skip some rows with-out any exception with in the code. 我完成了基于文本文件的日志记录,并观察到for-each循环跳过了一些行,而代码中没有任何异常。 I have a log in the very first line when loop begins which also not working for skipped items. 当循环开始时,我在第一行有一个日志,这对于跳过的项目也不起作用。

below is my code: 下面是我的代码:

foreach (DataRow item in tblItemsToIssue.Rows)
        {
            Logger.LogInfo(string.Format("Start processing item = {0}", item["Item_Id"].ToString()));

            Dept_Item_BatchWise dibw = new Dept_Item_BatchWise();
            KeyValueCollection kvd = new KeyValueCollection();
            kvd.Add("Item_Id", Convert.ToInt32(item["Item_Id"]));
            kvd.Add("Department_Id", Convert.ToInt32(SessionObject.Get("User_Department_Id").ToString()));
            DataTable dt = dibw.GetFiltered_Table(kvd, true, "Expiry_Date", true);

            if (dt.Rows.Count > 0)
            {

                Logger.LogInfo("Batches found");

                //Check if available batches have enough quantity to issue
                int BatchesQuantitySum = 0;
                foreach (DataRow bItem in dt.Rows)
                {
                    BatchesQuantitySum += Convert.ToInt32(bItem["Quantity"]);
                }

                int QuantityNeedToIssue = Convert.ToInt32(item["Issue_Quantity"]);

                if (BatchesQuantitySum >= QuantityNeedToIssue)
                {
                    string documentDetailId = Convert.ToString(itemsDetailPharmacyIssuanceIds.GetByIndex(itemsDetailPharmacyIssuanceIds.IndexOfKey(item["Item_Id"])));

                    int QuantityInBatch = Convert.ToInt32(dt.Rows[0]["Quantity"]);

                    if (QuantityNeedToIssue > QuantityInBatch)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (QuantityNeedToIssue > 0)
                            {
                                int batchQty = Convert.ToInt32(dt.Rows[i]["Quantity"]);
                                if (batchQty <= 0) //skip current batch coz it has 0 quantity
                                    continue;

                                int iQty = 0;

                                if (QuantityNeedToIssue <= batchQty)
                                {
                                    iQty = QuantityNeedToIssue;
                                    QuantityNeedToIssue -= iQty;
                                }
                                else
                                {
                                    iQty = batchQty;
                                    QuantityNeedToIssue -= iQty;
                                }

                                //Item Transaction Made
                                Dept_Item_Transaction dti = new Dept_Item_Transaction();
                                KeyValueCollection kv = new KeyValueCollection();
                                kv.Add("Document_Id", documentId);
                                kv.Add("Document_No", documentNo);
                                kv.Add("Document_Date", documentDate);
                                kv.Add("Document_Detail_Id", documentDetailId);
                                kv.Add("Item_Id", item["Item_Id"].ToString());
                                kv.Add("Unit_Id", item["Unit_Id"].ToString());
                                kv.Add("In_Out", "O");
                                kv.Add("Active", "Y");
                                kv.Add("Department_Id", Convert.ToInt32(SessionObject.Get("User_Department_Id").ToString()));
                                kv.Add("Unit_Price", item["Unit_Price"].ToString());
                                kv.Add("Employee_Id", SessionObject.Get("User_Employee_Id"));
                                kv.Add("Quantity", iQty);
                                if (DBNull.Value != dt.Rows[i]["Expiry_Date"] && !string.IsNullOrEmpty(dt.Rows[i]["Expiry_Date"].ToString()))
                                {
                                    DateTime t;
                                    DateTime.TryParse(dt.Rows[i]["Expiry_Date"].ToString(), out t);

                                    if (t != DateTime.MinValue)
                                        kv.Add("Expiry_Date", dt.Rows[i]["Expiry_Date"].ToString());
                                }
                                if (DBNull.Value != dt.Rows[i]["Batch_No"])
                                    kv.Add("Batch_No", dt.Rows[i]["Batch_No"].ToString());

                                dti.PerformTransaction(DocumentType.Issuance, kv);

                                Logger.LogInfo(string.Format("Issuance from Batch: {0}", dt.Rows[i]["Batch_No"].ToString()));
                            }
                            else
                            {
                                Logger.LogInfo("Multi batch issuance done.");
                                return;
                            }
                        }
                    }
                    else
                    {
                        //Item Transaction Made
                        Dept_Item_Transaction dti = new Dept_Item_Transaction();
                        KeyValueCollection kv = new KeyValueCollection();
                        kv.Add("Document_Id", documentId);
                        kv.Add("Document_No", documentNo);
                        kv.Add("Document_Date", documentDate);
                        kv.Add("Document_Detail_Id", documentDetailId);
                        kv.Add("Item_Id", item["Item_Id"].ToString());
                        kv.Add("Unit_Id", item["Unit_Id"].ToString());
                        kv.Add("In_Out", "O");
                        kv.Add("Active", "Y");
                        kv.Add("Department_Id", Convert.ToInt32(SessionObject.Get("User_Department_Id").ToString()));
                        kv.Add("Unit_Price", item["Unit_Price"].ToString());
                        kv.Add("Employee_Id", SessionObject.Get("User_Employee_Id"));
                        if (DBNull.Value != dt.Rows[0]["Expiry_Date"] && !string.IsNullOrEmpty(dt.Rows[0]["Expiry_Date"].ToString()))
                        {
                            DateTime t;
                            DateTime.TryParse(dt.Rows[0]["Expiry_Date"].ToString(), out t);

                            if (t != DateTime.MinValue)
                                kv.Add("Expiry_Date", dt.Rows[0]["Expiry_Date"].ToString());
                        }
                        if (DBNull.Value != dt.Rows[0]["Batch_No"])
                            kv.Add("Batch_No", dt.Rows[0]["Batch_No"].ToString());
                        kv.Add("Quantity", QuantityNeedToIssue);
                        dti.PerformTransaction(DocumentType.Issuance, kv);

                        Logger.LogInfo("Single Batch Issuance");
                    }
                }
                else
                {
                    Logger.LogInfo("Not available enoungh in stock : Process Terminate");

                    string error = string.Format("Internal error: Item Id '{0}' has not available enough in stock to issue.", item["Item_Id"].ToString());
                    throw new Exception(error);
                }
            }
            else
            {
                int QuantityNeedToIssue = Convert.ToInt32(item["Issue_Quantity"]);

                if (QuantityNeedToIssue > 0)
                {
                    Logger.LogInfo("No batches found : Process Terminate");

                    string error = string.Format("Internal error: Item Id '{0}' is missing from table Dept_Item_Batchwise.", item["Item_Id"].ToString());
                    throw new Exception(error);
                }
                else
                {
                    Logger.LogInfo("Item Skip: Issue quantity is 0");
                }
            }
        }

LOG: 日志:

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Before save document total items count = 20

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
After save document total items count = 20

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Before save transaction total items count = 20

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 356

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 397

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1281

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 579

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 3195

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1886

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1845

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1080

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 3385

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 2702

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1453

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Single Batch Issuance

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Start processing item = 1448

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Batches found

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Issuance from Batch: 144

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Multi batch issuance done.

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
After save transaction total items count = 20

===== INFO ===== 26 Jun 2015 - 00:34:34 ===== Kacho Nisar (23) ===== IP:10.10.10.67 =====
Transaction committed

Possible solution: 可能的解决方案:

Copy tblItemsToIssue to a local (in-memory) table just before the foreach . tblItemsToIssue复制到foreach之前的本地(内存中)表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM