Retrieve object from an arraylist with a specific

2019-09-05 22:46发布

I have a class with some attributes. For example class person with name, age, gender and so on as attribute.

People fill out a form, with among other things their gender which is a dropdownlist, they submit it and the person is added to the arraylist.

What I need is a function by clicking a button to display all the persons who chose Female as gender.

Can somebody please help me? I have tried and searched for the right answer for days now and getting a little desperat now.

Many thanks!!!

Olaf

This is my .cs code

    public class Person
{
    private string name;
    private string artistname;
    private string address;
    private double number;
    private double zip;
    private string day;
    private string gender;

public Person(string name, string artistname, string address, double number, double zip, string day, string gender)
    {
        this.name = name;
        this.artistname = artistname;
        this.address = address;
        this.number = number;
        this.zip = zip;
        this.day = day;
        this.gender = gender;
    }

    public override string ToString()
    {
        string newPerson = name + " aka " + artistname + " lives on " + address + " " + number + " " + zip + " " + day + "Gender: " + gender;
        return newPerson;
    }
}

And this is my .aspx code:

public partial class Index : System.Web.UI.Page
{
    static ArrayList personArrayList;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            personArrayList = new ArrayList();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Person p = new Person(txtName.Text, txtArtistName.Text, txtAddress.Text, Convert.ToDouble(txtNumber.Text), Convert.ToDouble(txtPostal.Text), Convert.ToString(dropdownDay.Text), Convert.ToString(dropdownGender.Text));
        personArrayList.Add(p);
    }

    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        ListBoxShow.Items.Clear();
        for (int i = 0; i < personArrayList.Count; i++)
        {
            ListBoxShow.Items.Add(personArrayList[i].ToString());
        }
    }

    protected void btnShowGentle_Click(object sender, EventArgs e)
    {
        ListBoxShow.Items.Clear();

    }

    protected void btnShowLadies_Click(object sender, EventArgs e)
    {
        ListBoxShow.Items.Clear();
        for (int i = 0; i < personArrayList.Count; i++)
        {
            if (personArrayList[i].gender = "Female")
            {

            }
        }
    }
}

3条回答
Explosion°爆炸
2楼-- · 2019-09-05 23:17

Do you thank something like that?

protected void btnShowGentle_Click(object sender, EventArgs e)
{
    ListBoxShow.Items.Clear();
    for (int i = 0; i < personArrayList.Count; i++)
    {
        if(personArrayList[i].gender == "Male")
            ListBoxShow.Items.Add(personArrayList[i].ToString());
    }
}

////Possible alternate
//ListBoxShow.Items.Clear();
//ListBoxShow.Items.AddRange(personArrayList.Where( x => x.gender == "Male"));
查看更多
够拽才男人
3楼-- · 2019-09-05 23:21

I think something like this should do the trick

using System.Linq;
var females = from Person P in personArrayList where P.Gender == "Female" select P;

[Edit] I had some questions about this myself so asked a question Plain ArrayList Linq c# 2 syntaxes (need a conversion) that can be useful to you.

查看更多
来,给爷笑一个
4楼-- · 2019-09-05 23:25

The C# way is to take usage of LINQ to query collections, as such:

var persons = personArrayList.AsQueryable();
var females = persons.Where(p => p.gender.Equals("Female"));
查看更多
登录 后发表回答