删除重复项,并把列表转换成一个列表框 [英] Remove duplicates and put the list into a listbox

查看:104
本文介绍了删除重复项,并把列表转换成一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个单向的分配和我有其中的一部分问题。这是代码;

This is an uni assignment and I am having problem with part of it. This is the code;

namespace Assignment_1
{
public partial class Classifier : System.Web.UI.Page // We are using a web form as stated
{
    protected void Page_Load(object sender, EventArgs e) // No variables are initiated for the beginning
    {

    }



    protected void ButtonClassify_Click(object sender, EventArgs e)
    {

        if (this.TextBox1.Text != "")
        {
            List<string> numbersText = this.TextBox1.Text.Split(',').ToList<string>();
            foreach (var item in numbersText)
            {
                int num = int.Parse(item);


                if (RadioButtonList1.SelectedValue == "Both") 
                {
                    if (num % 2 == 0)
                    {
                        if (CheckBoxDuplicate.Checked == true)
                        {
                            List<int> evenNumbers = new List<int>();
                            evenNumbers.Add(num);
                            List<int> distinctEvenNumbers = evenNumbers.Distinct().ToList();
                            ListBoxEvenNumbers.DataSource = distinctEvenNumbers;

                        }
                        else
                        {

                            //Put the results into the respective boxes
                            ListBoxEvenNumbers.Items.Add(num.ToString());
                        }

                    }
                    else
                    {
                        //Put the results into the respective boxes
                        ListBoxOddNumbers.Items.Add(num.ToString());
                    }
                }

                if (RadioButtonList1.SelectedValue == "Even")
                {
                    if (num % 2 == 0)
                    {
                        //Put the results into the respective boxes
                        ListBoxEvenNumbers.Items.Add(num.ToString());
                    }
                }

                if (RadioButtonList1.SelectedValue == "Odd")
                {
                    if (num % 2 == 1)
                    {
                        //Put the results into the respective boxes
                        ListBoxOddNumbers.Items.Add(num.ToString());
                    }
                }

让我解释一下这个问题,我做了什么。用户将号码列表到一个文本框,然后有3个选项(radiolistbutton)。他可以列出偶,奇或两种类型的数字。他们偶数和奇数列表框(2列表框)显示。我已经做了这部分。

Let me explain the question and what I have done. User inserts list of numbers into a text box and then has 3 options (radiolistbutton). He can list even, odd or both type of numbers. They display in even and odd listboxes(2 listboxes). I have done up to this part.

有一个复选框,删除重复,如果他愿意,用户可以检查它。如果按钮被选中,代码应该删除重复。我试图在第四做到这一点的一部分的if-else,如果(CheckBoxDuplicate.Checked == true)而。我的理解是这样的,我检查,如果数字为偶数,然后检查CheckboxDuplicate按钮。如果它被选中我把值一个新的列表,然后删除重复的值。然后放入EvenNumbers列表框。出于某种原因,这部分不能正常工作。

There is a checkbox to remove duplicates and the user can check it if he wishes to. If the button is checked, the code should remove the duplicates. I tried to do this part in the 4th "if-else" "if (CheckBoxDuplicate.Checked == true)". The way I understand it, I check if the number is even and then check the CheckboxDuplicate button. if it is checked I put the values in a new list and then delete repeated values. Then put into EvenNumbers listbox. For some reason, this part doesn't work.

如果你想帮助我,请不要张贴只是你的答案。这是我在C#中的第一个项目,这是我很难理解一个优雅的解决方案呢。如果你有时间,请检查我的代码,让我知道我犯了一个错误。感谢您的时间提前。

If you would like to help me, please don't post just your answer. This is my first project in C# and it is difficult for me to understand an elegant solution yet. If you have time, please check my code and let me know where I made a mistake. Thanks for your time in advance.

推荐答案

洒在这一点LINQ魔法,就大功告成了。

Sprinkle a bit of linq magic on it, and you're done.

var my_list = new List<int>{1,2,3,4,5,5,6,7};

var pair = my_list.Where (n => n%2 ==0); // 2,4,6
var odd = my_list.Where (n => n%2 ==1);  // 1,3,5,5,7

var unique_pair = pair.Distinct(); // stays the same
var unique_odd = odd.Distinct();   // 1,3,5,7



从这里只是将它添加到您的适当的调用和GUI容器

From here is just adding it to your appropriate calls and GUI containers

从您的评论,这里有几件事情:

From your comment, here are a couple of things:


  1. 更改如果如果 - 否则如果,因为只有一个适用

  2. 您可以做到这一点,你做的方式,但它不是最有效的。如果你走那条路,你必须搞清楚你为了不重复已经添加了哪些号码。

  3. 另外,你可以简单地创建列表就像我已经做了在上面的代码,然后在最后分配。这将节省您的时间和代码。

  1. Change the if to if - else if, since only one will apply.
  2. you can do it the way you do, but it's not the most efficient. If you go that way, you'll have to figure out which numbers you've added in order to not have duplicates.
  3. alternatively, you can simply create the lists like I've done in the code above, and then assign them at the end. It'll save you time and code.

下面是一些更多的帮助,没有代码,因为我相信我遮盖


Here's some more help, with no code, since I believe i covered it.

第1步:获取用户输入,并创建整数列表。 (称之为:input_list)。结果
第2步:据他选择了(偶,奇,两者),要分配给每个列表框,数字列表。看看我上面的代码,它会做该位为你搜索
第3步:如果用户的动产独一无二的,你传递给这些列表框的鲜明列表中,再次,看我为上面的代码。

Step 1: get the user input, and create a list of ints. (call it: input_list).
Step 2: According to what he chose (even, odd, both), you want to assign to each listbox, a list of numbers. Look at my above code, it'll do that bit for you.
Step 3: If user choses unique, you pass to those listboxes the Distinct list, again, look at my above code for that.

您可以将独特的复选框的情况下,如果你喜欢被选中

You can apply the unique on the event of the checkbox being selected if you prefer.

注:结果
保留整数(该input_list)作为变量的列表,因此你不需要时,他改变了解析它他的选择。

Notes:
Keep the list of ints (the input_list) as a variable, so you don't need to parse it whenever he changes his selection.

这篇关于删除重复项,并把列表转换成一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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