Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c# - Retrieve object from an arraylist with a specific element value

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")
            {

            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...