[英]C# Find top 3 in Collection list box
查找前三名按鈕-處理數據以查找前三位的最高銷售額,並顯示這些銷售人員的姓名以及他們的位置(第一,第二,第三)和銷售額。
我有兩個列表框,我只想查找其銷售額最高的前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
// ... ??
}
// ...
}
謝謝
就像是
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 ...在您的循環中,我假設您有一個帶有行的文本文件,並且每個名稱和銷售價值...最后,我已經添加了一個創建到您的lstSalesPerson
(頂部添加)中的SalesPerson
。
現在我們快到了……請閱讀並理解本文: http : //www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele 。
完成后,您將意識到不需要lstTotalSales
和lstNames
,因為您只需將這些列表框鏈接到lstSalesPerson
的DisplayMemberPath
lstSalesPerson
。
現在,當您使用上面的linq選擇前3個對象時,就有3個對象。 對於每個對象,可以使用值Name
和TotalSales
因為它們只是該對象的屬性。
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.