去除ListBox中重复的项目(字符串) [英] Removing duplicate items (string) from listBox

查看:810
本文介绍了去除ListBox中重复的项目(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名大学生正在为一个单位的一个项目。我们要创建一个Web应用程序仪表板,我似乎无法从列表框(customerNameListBox)删除重复。

 公共部分类图:System.Web.UI.MasterPage
    {
        字符串FullDataCSV = Path.Combine(HttpContext.Current.Server.MapPath
    (〜/ App_Data文件/ Full_Data.csv));    清单< CSVEntry> CSVList =新的List< CSVEntry>();    公共无效ReadFile的()
    {
        尝试
        {
            StreamReader的INPUTFILE;
            串线;
            CSVEntry进入=新CSVEntry();
            的char [] = DELIM {','};
            INPUTFILE = File.OpenText(FullDataCSV);            而(!inputFile.EndOfStream)
            {
                行= inputFile.ReadLine();
                的String []标记= line.Split(DELIM);
                entry.Value0 =令牌[0];
                entry.customerName =令牌[22];
                entry.Value29 =令牌[29];                CSVList.Add(输入);
            }
        }
        抓住
        {
            的Response.Redirect(Error.aspx);
        }
    }    私人无效DisplayCustomerName()
    {
        的foreach(在CSVList CSVEntry进入)
        {
            customerNameListBox.Items.Add(entry.customerName);
        }
    }    私人无效SortCustomerName()
    {
        CSVList = CSVList.OrderBy(X => x.customerName).ToList();
    }    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        ReadFile的();
        SortCustomerName();
        DisplayCustomerName();
    }    保护无效historyButton_Click(对象发件人,EventArgs的发送)
    {
        的Response.Redirect(History.aspx);
    }    保护无效exportButton_Click(对象发件人,EventArgs的发送)
    {
        的Response.Redirect(Export.aspx);
    }    保护无效printButton_Click(对象发件人,EventArgs的发送)
    {
        的Response.Redirect(Print.aspx);
    }
}

我已经使用下面的code以去除customerNameTextBox重复的项目尝试,但它不工作。

 保护无效goButton_Click(对象发件人,EventArgs的发送)
    {
        清单<串GT;清除=新的List<串GT;();
        的foreach(在customerNameListBox.Items字符串s)
        {
            removals.Add(多个);
        }        的foreach(在清除字符串s)
        {
            customerNameListBox.Items.Remove(多个);
        }


解决方案

更新您的code和在下拉列表中添加项之前检查重复的。

 私人无效DisplayCustomerName()
{
    的foreach(在CSVList CSVEntry进入)
    {
          列表项项=新的ListItem(entry.customerName);
         如果(!customerNameListBox.Items.Contains(项目))
         {
              customerNameListBox.Items.Add(项目);
         }
    }
}

现在,你将不需要从您的下拉列表中删除重复值。

甚至可以使用LINQ选择您的收藏不同的值。

I'm a university student doing a project for a unit. We're creating a web application dashboard, and I can't seem to remove duplicates from a 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");
    }
}

I have tried using the below code to remove the duplicate items in the customerNameTextBox, but it's not working at all.

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

解决方案

Update your code and check for duplicate before adding item in the dropdown list.

private void DisplayCustomerName()
{
    foreach (CSVEntry entry in CSVList)
    {
          ListItem item = new ListItem(entry.customerName);
         if ( ! customerNameListBox.Items.Contains(item) )
         {
              customerNameListBox.Items.Add(item);
         }
    }
}

Now you will not require to delete duplicate values from your dropdown list.

Or even you can select distinct values in your collection using linq.

这篇关于去除ListBox中重复的项目(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆