【问题标题】:C# Find top 3 in Collection list boxC# 在集合列表框中查找前 3 名
【发布时间】:2013-11-14 02:38:17
【问题描述】:

Find Top 3 Button – 处理数据以找到 3 个最高销售额 并显示这些销售人员的姓名以及他们的位置(1st、2nd、3rd), 和销售额。

我有两个列表框,我只想找到销售额最高的前 3 名销售人员,并将他们作为消息框显示给用户,分 3 行显示

应用图片: http://s17.postimg.org/6dvo3a4qn/Untitled.jpg

列表框名称:lstNames、lstTotalSales

我找到前 3 个按钮的代码是:

private void btnFindTop3_Click(object sender, EventArgs e)
{
    decimal dec1HighestAmount = 0;
    decimal dec2HighestAmount = 0;
    decimal dec3HighestAmount = 0;
    for (int Index = 0; Index < lstTotalSales.Items.Count; Index++)
    {
        if (Convert.ToDecimal(lstTotalSales.Items[Index]) > dec1HighestAmount)
        {
            dec1HighestAmount = Convert.ToDecimal(lstTotalSales.Items[Index]);
        }
        if (Convert.ToDecimal(lstTotalSales.Items[Index]) < dec1HighestAmount)
        {
            dec2HighestAmount = Convert.ToDecimal(lstTotalSales.Items[Index]);
        }
        if (Convert.ToDecimal(lstTotalSales.Items[Index]) < dec2HighestAmount)
        {
            dec2HighestAmount = Convert.ToDecimal(lstTotalSales.Items[Index]);
        }
    }
    MessageBox.Show("Highest Amount is " + dec1HighestAmount + " and " + dec2HighestAmount + " and " + dec3HighestAmount);
}

这是我用来填充列表框的代码:

public partial class Form1 : Form
{
    List<decimal> lstTotal = new List<decimal>();
    public Form1()
    {
        InitializeComponent();
    }

    private void btnReadInSalesData_Click(object sender, EventArgs e)
    {
        openFileDialog1.FileName = "SalesNumbers.txt";
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) //If and Open Dialog OK
        {
            StreamReader srFile = File.OpenText(openFileDialog1.FileName);

            decimal decTotal = 0;

            while (!srFile.EndOfStream)
            {
                string strline = srFile.ReadLine();
                string[] strSplit = strline.Split('$');

                foreach (string strSplittedOutput in strSplit)
                {

                    if (decimal.TryParse(strSplittedOutput, out decTotal))
                    {
                        lstTotal.Add(decTotal);
                        lstTotalSales.Items.Add(strSplittedOutput);
                    }
                    else //else than decimals add strings
                    {
                        lstNames.Items.Add(strSplittedOutput); //add the Sales men names to lstNames listbox
                    }
                }
            } //End of while

            srFile.Close(); //Close StreamReader
        }
        else
            MessageBox.Show("User Cancel Read File Operation."); // if the user cancel the read file operation show this messagebox

        // ... ??
    }

    // ...
}

谢谢

【问题讨论】:

  • 问题是..?
  • 而不是找到前 3 名,为什么不对列表进行排序呢?以防万一这个程序的用户想要找到前 5 名、前 10 名等。
  • 这是消息框的输出:s22.postimg.org/tb90fml0x/Untitled2.jpg,我只是想获取 lstNames 中人名的索引号,该索引号等于显示在消息框中的前 3 个。谢谢

标签: c# listbox find highest


【解决方案1】:

类似

var top_three = (from sales in lstTotalSales.Items
                 orderby sales descending
                 select sales).Take(3);

将使用 linq 保持前 3 名...


啊……好吧……坏消息……你还有很多工作要做……好消息,这会让你走上正确的道路:

public partial class Form1 : Form
{
    List<decimal> lstTotal = new List<decimal>();
    List<SalesPerson> lstSalesPerson = new List<SalesPerson>;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnReadInSalesData_Click(object sender, EventArgs e)
    {
        openFileDialog1.FileName = "SalesNumbers.txt";
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) //If and Open Dialog OK
        {
            StreamReader srFile = File.OpenText(openFileDialog1.FileName);

            decimal decTotal = 0;
            decimal tempSales =0;
            string tempName = "";

            while (!srFile.EndOfStream)
            {
                string strline = srFile.ReadLine();
                string[] strSplit = strline.Split('$');

                // I'll seriously assume this happens only twice ...
                foreach (string strSplittedOutput in strSplit)
                {

                    if (decimal.TryParse(strSplittedOutput, out decTotal))
                    {
                        lstTotal.Add(decTotal);
                        lstTotalSales.Items.Add(strSplittedOutput);
                        tempSales = strSplittedOutput;
                    }
                    else //else than decimals add strings
                    {
                        lstNames.Items.Add(strSplittedOutput); //add the Sales men names to lstNames listbox
                        tempName = strSplittedOutput;
                    }
                }

                    // Adding this to our people list ...
                    lstSalesPerson.Add(new SalesPerson {Name=tempName,TotalSales=tempSales});

            } //End of while

            srFile.Close(); //Close StreamReader
        }
        else
            MessageBox.Show("User Cancel Read File Operation."); // if the user cancel the read file operation show this messagebox

        // ... ??
    }

    // ...
}


public class SalesPerson {
    public string Name {get; set;}
    public decimal TotalSales {get; set;}
}

我添加了一个名为 SalesPerson 的类 ...这个对象有一个 Name 和一个 TotalSales ...doooohhh ...在你的循环中,我假设你有一个带有线条的文本文件和在每个名称和销售价值中...我在最后添加了一个 SalesPerson 的创建,该 lstSalesPerson 进入您的 lstSalesPerson(添加在顶部)。

现在我们快到了……请阅读并理解这篇文章:http://www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele

一旦你这样做了,你就会意识到你不需要lstTotalSaleslstNames,因为你可以将这些列表框链接到你的lstSalesPersonDisplayMemberPath

现在,当您使用上面的 linq 选择前 3 个对象时,您有 3 个对象。对于每个对象,您可以使用值 NameTotalSales,因为它们只是该对象的属性。

var top_three = (from person in lstSalesPerson
             orderby person.TotalSales descending
             select person).Take(3);

foreach (var person in top_three) {
     // do something with person.Name
     // do something with person.TotalSales
}

【讨论】:

  • 谢谢你,我也想知道他们的名字和销售额!
  • 所以?如果您有一组人,并且每个对象都包含名称、销售额和他们小狗的颜色,您可以让每个列表使用显示值仅显示一个属性,但在主列表上运行该 linq,将获得对象.然后只需引用您需要的属性,例如名称和销售额...
  • 看来你没有明白我的意思或者我无法理解你的意思,我有两个不同的列表框,linkq 将在不知道人名的情况下获得总销售额。请进一步解释。谢谢你的时间:)
  • Listboxes names: lstNames, lstTotalSales 我不知道如何使c#循环获取列表框中人名的相同索引号,等于销售额索引号。我希望这对你来说足够清楚。谢谢你:)
  • 您如何填写这些列表?您必须有一个包含姓名和销售属性的人员集合,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多