C# IEnumerable集合的重叠打印
我正在将XML文件的内容读入IEnumerable集合(数组),我需要在单独的页面上打印每个迭代(类似XML数据块) 我正在使用Print()函数和e.HasMorePages。我的问题是foreach在每次打印的IEnumerable集合的所有迭代中循环,因此我打印的是正确的页数,但每页都包含所有迭代,而不是每页一次。有人能给我一个解决方案或更好的方法来管理这个过程吗 下面是代码的相关部分C# IEnumerable集合的重叠打印,c#,printing,ienumerable,C#,Printing,Ienumerable,我正在将XML文件的内容读入IEnumerable集合(数组),我需要在单独的页面上打印每个迭代(类似XML数据块) 我正在使用Print()函数和e.HasMorePages。我的问题是foreach在每次打印的IEnumerable集合的所有迭代中循环,因此我打印的是正确的页数,但每页都包含所有迭代,而不是每页一次。有人能给我一个解决方案或更好的方法来管理这个过程吗 下面是代码的相关部分 // Print Employee General info foreach (Empl
// 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);
}
//打印员工一般信息
foreach(GEmployeeXGD.GetEmployeeGeneralData()中的EmployeeInfo itm)
{
尝试
{
empFirstName=itm.FirstName;
empLastName=itm.LastName;
empMidInitial=itm.middinitial;
等等…
//为每个员工设置字段坐标
//*******员工的一般信息********
PointF empFirstNameLoc=新的PointF(430271);
PointF empLastNameLoc=新的PointF(600271);
PointF empMidInitialLoc=新的PointF(563271);
等等…
//发送字段文本数据
使用(字体courierFont=新字体(“Courier”,10,FontStyle.Bold))
{
e、 图形.抽绳(empFirstName,courierFont,笔刷.黑色,empFirstNameLoc);
e、 图形.抽绳(empLastName,courierFont,Brush.Black,empLastNameLoc);
e、 图形.抽绳(empMidInitial,courierFont,笔刷.黑色,empmidinitialoc);
等等…
}
}
catch(异常错误){MessageBox.Show(error.ToString());}
e、 HasMorePages=(记录
这很有帮助,乔尔,谢谢
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;
}
公共数组GetEmployeeGeneralData()
{
//XML源文件
var xmlEmployeeFile=File.ReadAllText(“Corrections.xml”);
XDocument employeeDoc=XDocument.Parse(xmlEmployeeFile);
XElement w2cEmployeeDat=employeeDoc.Element(“更正数据”);
EmployeeInfo[]employeeGenInfo=null;
if(w2cEmployeeDat!=null)
{
IEnumerable employeeRecords=w2cEmployeeDat.Elements(“员工”);
尝试
{
employeeGenInfo=(来自employeeRecords中的itm
选择新员工信息()
{
FirstName=(itm.Element(“FirstName”)!=null)?itm.Element(“FirstName”)。值:string.Empty,
LastName=(itm.Element(“LastName”)!=null)?itm.Element(“LastName”)。值:string.Empty,
midinital=(itm.Element(“midinital”)!=null)?itm.Element(“midinital”)。值:string.Empty,
等等…
}).ToArray();
}
catch(异常){MessageBox.Show(error.ToString());}
}
SetRecordCount(employeeGenInfo.Count());
recordCount=Globals.totalRecordCount;
返回employeeGenInfo;
}
您需要跟踪您所在的页面,然后执行以下操作:
var items = GEmployeeXGD.GetEmployeeGeneralData().Skip( (pageNumber-1) * numberItemsPerPage).Take(numberOfItemsPerPage);
foreach (EmployeeInfo itm in items)
{
//...
}
如果您可以避免反复调用相同的方法,并在各个页面上保持相同的IEnumerable,则更好。但为了演示这一点,我需要更多地了解该方法所在类的上下文,并了解更多有关
GEmployeeXGD
的工作原理。Skip不适用于GEmployeeXGD.GetEmployeeGeneralData()。如果我按指示赋值(var items=GEmployeeXGD.GetEmployeeGeneralData();),那么items包含集合的所有数组元素(即employeeInfo[0],employeeInfo[1],等等)。有没有办法索引这些元素,以便foreach一次只使用一个?