【问题标题】:Overlapping printing of an IEnumerable collectionIEnumerable 集合的重叠打印
【发布时间】:2017-12-17 03:04:28
【问题描述】:

我正在将 XML 文件的内容读入 IEnumerable 集合(数组),我需要在单独的页面上打印每个迭代(类似 XML 数据的块)。

我正在使用 Print() 函数和 e.HasMorePages。我的问题是 foreach 循环遍历每次打印的 IEnumerable 集合的所有迭代,因此我打印了正确的页数,但每页包含所有迭代,而不是每页一个。谁能给我一个解决方案或更好的方法来管理这个过程的想法?

这是代码的相关部分...

// Print Employee General info
        foreach (EmployeeInfo itm in GEmployeeXGD.GetEmployeeGeneralData())
        {
            try
            {
                empFirstName = itm.FirstName;
                empLastName = itm.LastName;
                empMidInitial = itm.MidInitial;
        etc…

                // Set field coordinates for each employee
                // ******* Employee's general information ********
                PointF empFirstNameLoc = new PointF(430, 271);
                PointF empLastNameLoc = new PointF(600, 271);
                PointF empMidInitialLoc = new PointF(563, 271);
        etc…
                // Send field text data
                using (Font courierFont = new Font("Courier", 10, FontStyle.Bold))
                {
                    e.Graphics.DrawString(empFirstName, courierFont, Brushes.Black, empFirstNameLoc);
                    e.Graphics.DrawString(empLastName, courierFont, Brushes.Black, empLastNameLoc);
                    e.Graphics.DrawString(empMidInitial, courierFont, Brushes.Black, empMidInitialLoc);
        etc…
                }
            }
            catch (Exception error) { MessageBox.Show(error.ToString()); }
            e.HasMorePages = (records < Globals.totalRecordCount);
        }

这对乔尔很有帮助,谢谢。

GEmployeeXGD 尊重具有单一方法的类。该方法读取我需要的 XML 数据并将 IEnumerable 集合填充为数组。方法是这样的。。

        public Array GetEmployeeGeneralData()
    {
        // XML source file
        var xmlEmployeeFile = File.ReadAllText("Corrections.xml");
        XDocument employeeDoc = XDocument.Parse(xmlEmployeeFile);
        XElement w2cEmployeeDat = employeeDoc.Element("CorrectedDAta");
        EmployeeInfo[] employeeGenInfo = null;
        if (w2cEmployeeDat != null)
        {
            IEnumerable<XElement> employeeRecords = w2cEmployeeDat.Elements("Employee");
            try
            {
                employeeGenInfo = (from itm in employeeRecords
                                   select new EmployeeInfo()
                                   {
                                       FirstName = (itm.Element("FirstName") != null) ? itm.Element("FirstName").Value : string.Empty,
                                       LastName = (itm.Element("LastName") != null) ? itm.Element("LastName").Value : string.Empty,
                                       MidInitial = (itm.Element("MidInitial") != null) ? itm.Element("MidInitial").Value : string.Empty,
            etc…
                                   }).ToArray<EmployeeInfo>();
            }
            catch (Exception) { MessageBox.Show(error.ToString());  }
        }
        Globals.SetRecordCount(employeeGenInfo.Count<EmployeeInfo>());
        recordCount = Globals.totalRecordCount;

        return employeeGenInfo;
    }

【问题讨论】:

    标签: c# printing ienumerable


    【解决方案1】:

    您需要跟踪您所在的页面,然后执行以下操作:

    var items = GEmployeeXGD.GetEmployeeGeneralData().Skip( (pageNumber-1) * numberItemsPerPage).Take(numberOfItemsPerPage); 
    foreach (EmployeeInfo itm in items)
    {
        //...
    }
    

    如果您可以避免一遍又一遍地调用相同的方法,并在各个页面之间保持相同的 IEnumerable,那就更好了。但是为了证明这一点,我需要更多地了解这个方法所在的类的上下文,并更多地了解GEmployeeXGD 的工作原理。

    【讨论】:

    • 跳过不适用于 GEmployeeXGD.GetEmployeeGeneralData()。如果我按指示分配( var items = GEmployeeXGD.GetEmployeeGeneralData(); ),则 items 包含集合的所有数组元素(即 employeeInfo[0]、employeeInfo[1] 等)。有没有办法索引这些元素,以便 foreach 一次只使用一个?
    猜你喜欢
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多