我是一个大学生为单位做项目。我们正在创建一个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以获得编写问题示例代码的指导。 –