简体   繁体   English

C#在“收藏夹”列表框中找到前三名

[英]C# Find top 3 in Collection list box

Find Top 3 Button – which processes the data to find the 3 highest sales amounts and displays these sales persons names along with their place (1st, 2nd, 3rd), and sales amount. 查找前三名按钮-处理数据以查找前三位的最高销售额,并显示这些销售人员的姓名以及他们的位置(第一,第二,第三)和销售额。

I have two list boxes I just want to find the top 3 highest sales persons with their sales amount and show them as message box to the user in 3 separate lines 我有两个列表框,我只想查找其销售额最高的前3位销售人员,并将其作为消息框显示给用户,分3行显示

Picture of the application: http://s17.postimg.org/6dvo3a4qn/Untitled.jpg 应用程序图片: http : //s17.postimg.org/6dvo3a4qn/Untitled.jpg

Listboxes names: lstNames, lstTotalSales 列表框名称:lstNames,lstTotalSales

My find top 3 button code is: 我发现前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);
}

This is the code that I used to fill the listboxes: 这是我用来填充列表框的代码:

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

        // ... ??
    }

    // ...
}

Thank you 谢谢

something like 就像是

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

will hold your top 3 using linq... 将使用linq保持您的前3名...


Argh ... ok ... the bad news ... you have lots of work ahead of you ... the good news, this will set you on the right way: 嗯...好吧...坏消息...您还有很多工作要做...好消息,这将使您走上正确的道路:

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;}
}

I've added a class called SalesPerson ... this objects has a Name and a TotalSales ... doooohhh ... In your loop, I'm assuming you a have a text file with lines and in each one name and sales value ... I've added at the end the creation of a SalesPerson that goes into your lstSalesPerson (added at the top). 我添加了一个名为SalesPerson的类...该对象具有NameTotalSales ... doooohhh ...在您的循环中,我假设您有一个带有行的文本文件,并且每个名称和销售价值...最后,我已经添加了一个创建到您的lstSalesPerson (顶部添加)中的SalesPerson

Now we're almost there ... Please go read and understand this article: http://www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele . 现在我们快到了……请阅读并理解本文: http : //www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele

Once you do, you'll realize you don't need to have the lstTotalSales and the lstNames , because you can just link those listboxes to the DisplayMemberPath of your lstSalesPerson . 完成后,您将意识到不需要lstTotalSaleslstNames ,因为您只需将这些列表框链接到lstSalesPersonDisplayMemberPath lstSalesPerson

Now, when you select your top 3 using the linq above, you have 3 objects. 现在,当您使用上面的linq选择前3个对象时,就有3个对象。 For each object you can use the values Name and TotalSales since they are just properties on that object. 对于每个对象,可以使用值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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM