首页 > 解决方案 > 通过列表循环使用 foreach,以执行不同值的代码

问题描述

我正在尝试创建一个函数,该函数为我循环遍历的属性的每个不同值创建一 (1) 个 PDF 发票(使用PDFSharp)。List

我获取 的值string,并希望将其用作发票的标题(它是开具发票的企业名称)。

举例说明:在下表中,我想获取LicenseHolder1(一次)、LicenseHolder2(一次)和LicenseHolder3(一次)的值。

ID 许可证持有者ID 价值1 价值2 价值3
1 执照持有人1 66 44 UF-2107
2 执照持有人2 22 25 UF-2107
3 执照持有人2 62 24 UF-2107
4 执照持有人3 82 12 UF-2107
5 执照持有人3 6 77 UF-2107
6 执照持有人3 15 62 UF-2107

这是我正在使用的方法:

using (SqlConnection con = new(ConnectionString.connectionString))
using (SqlCommand cmd = new("spCreateInvoice", con) {CommandType = CommandType.StoredProcedure})
using (SqlDataAdapter da = new(cmd))
// this is the name of my SQL table
using (DataTable dt = new("tblTripsPerMonth")) 
using (DataSet ds = new()) {

    con.Open();
    da.Fill(dt);
    ds.Tables.Add(dt);

    /* adding the SQL table rows to my ObservableCollection (PaidTrips), 
    which is bound to properties in my holder class (PaidTrip): */

    foreach (DataRow dr in ds.Tables[0].Rows {

       PaidTrips.Add(new PaidTrip {

             LicenseHolderID = dr[0].ToString(),
             Value1 = (decimal)dr[1],
             Value2 = (decimal)dr[2],
             Value3 = dr[3].ToString(),
        });

        // Now I am instantiating a list, so that I can group
        List<PaidTrip> paidTrip = PaidTrips
            .GroupBy(p => new {p.LicenseHolderID})
            .Select(g => g.First())
            .ToList();
            
        // this is copied code from another question, and this is where I fall of
        foreach (PaidTrip PaidTrips in paidTrip) {

            foreach (var LicenseHolderID in PaidTrips.GetType().GetProperties()) {

                string n = LicenseHolderID.GetValue(PaidTrips).ToString();

如果我在这个抓取之后string n将它传递给,比如说, a MessageBox.ShowLicenseHolder1它应该显示 。但是,如果我尝试将它传递到 PDF 中,像这样......(从最后一个代码块继续)

                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
                PdfDocument pdf = new();
                PdfPage page = pdf.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(page);

                // drawing in the string value
                gfx.DrawString(n, new XFont("Arial", 40, XFontStyle.Bold), myColor, new XPoint(40, 250));

                // saving the PDF
                pdf.Save("C:\File\Path\TestPDF.pdf");
            }
        }
    }
}

... PDF 已创建,但我绘制的字符串不是用 填充LicenseHolder1,而是用其他列的(看似)随机值(如66UF-2107)填充77。怎么来的?

(哦,我知道这段代码只创建一 (1) 个 PDF 文件,我需要几个(对于LicenseHolderID列中的每个不同值一个 - 但这是另一天的问题)

标签: c#listforeachienumerablepdfsharp

解决方案


如上所示,您的数据表中是否有 id 列?您的第 0 列不会在那里使用 id 吗?

foreach (DataRow dr in ds.Tables[0].Rows {

   PaidTrips.Add(new PaidTrip {

         LicenseHolderID = dr[0].ToString(),
         Value1 = (decimal)dr[1],
         Value2 = (decimal)dr[2],
         Value3 = dr[3].ToString(),
    });

如果您按 LicenseHolderID 分组,则选择分组结果的第一个元素。这是故意的吗?

List<PaidTrip> paidTrip = PaidTrips
        .GroupBy(p => new {p.LicenseHolderID})
        .Select(g => g.First())
        .ToList();

这将本质上返回一个包含多个包含相同 LicenseHolderID 的付费旅行对象的新对象。

通常,如果您想使用组的字符串名称,您可以引用键值。

paidTrip.Key.ToString();
    

可能会更好用

    var paidTrip = PaidTrips
        .GroupBy(p => new {p.LicenseHolderID})
        .ToList();

    foreach (var tripFound in paidTrip)
    {
        string tripId = tripFound.Key.ToString();
        //Generate your PDF File.
    }

这应该为每个组提供一个文件。抱歉,如果我接错了你。


推荐阅读