【问题标题】:Need not to print Duplicates values using c# [closed]不需要使用 c# 打印重复值 [关闭]
【发布时间】:2017-05-17 12:18:32
【问题描述】:

这是我打印“m_USTByDoctor_OPDC.ml_trn_bill_item”的代码,值来自数据库。

for (int i = 0; i < m_USTByDoctor_OPDC.ml_trn_bill_item.Count;i++ )
   {

   int count = 0;
  for (int j = 0; j m_USTByDoctor_OPDC.ml_trn_bill_item.Count; j++)
                {
if(m_USTByDoctor_OPDC.ml_trn_bill_item[j].ItemName==m_USTByDoctor_OPDC.ml_trn_bill_item[i].ItemName)
                    {
                        count++;
                    }
                }

                Add_Cell(m_USTByDoctor_OPDC.ml_trn_bill_item[i].ItemName, ref tbl_summaryContent3, HELVETICA_BOLD_8_BLACK, false, Rectangle.ALIGN_LEFT, Rectangle.ALIGN_MIDDLE);
                Add_Cell(count.ToString(), ref tbl_summaryContent3, HELVETICA_NORMAL_8_BLACK, false, Rectangle.ALIGN_LEFT, Rectangle.ALIGN_MIDDLE);
            }

这是我在你那里得到的输出重复值我也需要消除它我该怎么做

   Abdomen & Pelvic Scan IP   3
    Abdomen & Pelvic Scan IP   3
    Abdomine Pelvic Scan - OP   5
    Abdomine Pelvic Scan - OP   5
    Anamoly Scan (Single) - OP  1
    Abdomine Pelvic Scan - OP  5
    Abdomine Pelvic Scan - OP  5
    Abdomine Pelvic Scan - OP  5
    ANC/Obstetrics Scan(OP)    1
    Abdomen & Pelvic Scan IP  3

【问题讨论】:

  • 如果您使用 C# 编码,为什么选择 Java 和 asp.net?
  • .Distinct() 也许?

标签: c#


【解决方案1】:

您可以使用GroupBy linq 扩展方法:

foreach (var group in m_USTByDoctor_OPDC.ml_trn_bill_item.GroupBy(i => i.ItemName))
{
    Add_Cell(group.Key, ref tbl_summaryContent3, HELVETICA_BOLD_8_BLACK, false, Rectangle.ALIGN_LEFT, Rectangle.ALIGN_MIDDLE);
    Add_Cell(group.Count().ToString(), ref tbl_summaryContent3, HELVETICA_NORMAL_8_BLACK, false, Rectangle.ALIGN_LEFT, Rectangle.ALIGN_MIDDLE);
}

【讨论】:

  • 我收到一个错误,因为错误 6 无法在此范围内声明名为“i”的局部变量,因为它会给“i”赋予不同的含义,而“i”已在“父级或当前的'范围来表示其他东西
  • 由于这段代码应该替换你的两个循环,i 应该可用 - 但如果你愿意,你可以为 lambda 选择另一个标识符。
  • 谢谢C Evenhuis...问题解决了
猜你喜欢
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2020-06-04
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
相关资源
最近更新 更多