简体   繁体   English

如何查找在列表中具有某个值的属性的位置

[英]How to find the postion of a property which is holding some value in a List

// Here i want to know the index of each property in a List Flux which is fetching the value from an excel because later i have to fetch the value of these properties from the List.The problem i am facing is that sometimes some properties are not having any value in a specified row during the execution so i am not able to fetch them in the form Flux[0].VMS,OnHold[0].VMS,Active[0].VMS etc. The index is ambiguous so i need to know the index of each property in a List which is having some value. //在这里我想知道List Flux中每个属性的索引,它是从excel中获取值的,因为稍后我必须从List中获取这些属性的值。我面临的问题是有时某些属性是在执行期间指定行中没有任何值,因此我无法以Flux [0] .VMS,OnHold [0] .VMS,Active [0] .VMS等形式获取它们。索引不明确,因此我需要知道具有某个值的List中每个属性的索引。

    List<Summary> Flux = new List<Summary>();//total influx of each property

                    if (str1 == "Influx" && str1 != "Grand Total")
                    {
                        for (rCnt = rCnt + 7; rCnt < range.Rows.Count; rCnt++)
                        {
                            str = (string)(range.Cells[rCnt, 1] as Excel.Range).Text;
                            if (str == "VMS")
                            {
                                Summary summary = new Summary();
                                summary.VMS = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);

                            }
                            if (str == "eService")
                            {
                                Summary summary = new Summary();
                                summary.eService = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);

                            }
                            if (str == "RES")
                            {
                                Summary summary = new Summary();
                                summary.Res = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "RSC")
                            {
                                Summary summary = new Summary();
                                summary.RSC = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "VFS")
                            {
                                Summary summary = new Summary();
                                summary.VFS = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "SYS 80")
                            {
                                Summary summary = new Summary();
                                summary.System80 = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }

                            if (str == "HCL")
                            {
                                Summary summary = new Summary();
                                summary.HCL = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }
                            if (str == "Others")
                            {
                                Summary summary = new Summary();
                                summary.Others = xlWorkSheet.Cells[rCnt, 5].Text;

                                Flux.Add(summary);
                            }

                        }
                        break;
                    }

     //while fetching the vaues of each property having some value is the below code,instead of hardcoding the index of each property which has some value, i want to store it somewhere and then fetch it while writing the values in a cell.

 xlWorkSheet.Cells[iCount, jCount] = Flux[0].VMS;        //Daily influx
xlWorkSheet.Cells[iCount + 2, jCount] = Convert.ToString(Convert.ToInt32(OnHold[0].VMS)-Convert.ToInt32(xlWorkSheet.Cells[iCount+4,jCount-1].Text)); //Tickets moved to hold    
xlWorkSheet.Cells[iCount+3, jCount] = Active[0].VMS;     // Ending Backlog
xlWorkSheet.Cells[iCount + 4, jCount] = OnHold[0].VMS;   //Ending Hold

Because you said that during execution only 1 property is assigned, my first thought is to just do the following. 因为您说过在执行过程中只分配了1个属性,所以我的第一个想法就是执行以下操作。 But this depends on how you fill in Active and OnHold ; 但是这取决于您如何填写ActiveOnHold ; you should just change those to work like I made this one work: 您应该像我所做的那样更改它们以使其工作:

Summary summary = new Summary();

                if (str1 == "Influx" && str1 != "Grand Total")
                {
                    for (rCnt = rCnt + 7; rCnt < range.Rows.Count; rCnt++)
                    {
                        str = (string)(range.Cells[rCnt, 1] as Excel.Range).Text;
                        if (str == "VMS")
                        {                                
                            summary.VMS = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "eService")
                        {
                            summary.eService = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "RES")
                        {
                            summary.Res = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "RSC")
                        {
                            summary.RSC = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "VFS")
                        {
                            summary.VFS = xlWorkSheet.Cells[rCnt, 5].Text;
                        }
                        if (str == "SYS 80")
                        {
                            summary.System80 = xlWorkSheet.Cells[rCnt, 5].Text;
                        }

                        if (str == "HCL")
                        {
                            summary.HCL = xlWorkSheet.Cells[rCnt, 5].Text;

                        }
                        if (str == "Others")
                        {
                            summary.Others = xlWorkSheet.Cells[rCnt, 5].Text;
                        }

                    }
                    break;
                }

 //while fetching the vaues of each property having some value is the below code,instead of hardcoding the index of each property which has some value, i want to store it somewhere and then fetch it while writing the values in a cell.

xlWorkSheet.Cells[iCount, jCount] = summary.VMS;        //Daily influx
xlWorkSheet.Cells[iCount + 2, jCount] = Convert.ToString(Convert.ToInt32(OnHold[0].VMS)-Convert.ToInt32(xlWorkSheet.Cells[iCount+4,jCount-1].Text)); //Tickets moved to hold    
xlWorkSheet.Cells[iCount+3, jCount] = Active[0].VMS;     // Ending Backlog
xlWorkSheet.Cells[iCount + 4, jCount] = OnHold[0].VMS;   //Ending Hold

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

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