我是一個大學生爲單位做項目。我們正在創建一個Web應用程序儀表板,我似乎無法從listBox(customerNameListBox)中刪除重複項。從列表框刪除重複的項目(字符串)
public partial class Graphs : System.Web.UI.MasterPage
{
string FullDataCSV = Path.Combine(HttpContext.Current.Server.MapPath
("~/App_Data/Full_Data.csv"));
List<CSVEntry> CSVList = new List<CSVEntry>();
public void ReadFile()
{
try
{
StreamReader inputFile;
string line;
CSVEntry entry = new CSVEntry();
char[] delim = { ',' };
inputFile = File.OpenText(FullDataCSV);
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
entry.Value0 = tokens[0];
entry.customerName = tokens[22];
entry.Value29 = tokens[29];
CSVList.Add(entry);
}
}
catch
{
Response.Redirect("Error.aspx");
}
}
private void DisplayCustomerName()
{
foreach (CSVEntry entry in CSVList)
{
customerNameListBox.Items.Add(entry.customerName);
}
}
private void SortCustomerName()
{
CSVList = CSVList.OrderBy(x => x.customerName).ToList();
}
protected void Page_Load(object sender, EventArgs e)
{
ReadFile();
SortCustomerName();
DisplayCustomerName();
}
protected void historyButton_Click(object sender, EventArgs e)
{
Response.Redirect("History.aspx");
}
protected void exportButton_Click(object sender, EventArgs e)
{
Response.Redirect("Export.aspx");
}
protected void printButton_Click(object sender, EventArgs e)
{
Response.Redirect("Print.aspx");
}
}
我已經嘗試使用下面的代碼來刪除customerNameTextBox中的重複項目,但它不工作。
protected void goButton_Click(object sender, EventArgs e)
{
List<string> removals = new List<string>();
foreach (string s in customerNameListBox.Items)
{
removals.Add(s);
}
foreach (string s in removals)
{
customerNameListBox.Items.Remove(s);
}
邊注:請不要寫那麼多沒用的意見(我去除了大部分來自你的代碼)。如果您的函數或變量名稱不表示您意圖重命名而不是添加註釋。 'ReadFile(); //讀取文件'是荒謬的。查看http://www.sscce.org以獲得編寫問題示例代碼的指導。 –