0

我有一个具有一些属性的类。例如以姓名、年龄、性别等为属性的类人。

人们填写表格,除其他事项外,他们的性别是一个下拉列表,他们提交表格并将此人添加到数组列表中。

我需要的是一个功能,通过单击一个按钮来显示所有选择女性作为性别的人。

有人能帮帮我吗?我已经尝试并寻找正确的答案好几天了,现在有点绝望了。

非常感谢!!!

奥拉夫

这是我的 .cs 代码

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

这是我的 .aspx 代码:

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

            }
        }
    }
}
4

3 回答 3

2

C# 的方式是使用 LINQ 来查询集合,例如:

var persons = personArrayList.AsQueryable();
var females = persons.Where(p => p.gender.Equals("Female"));
于 2013-10-19T19:22:13.260 回答
2

我认为这样的事情应该可以解决问题

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

[编辑] 我自己对此有一些疑问,所以问了一个对您有用的问题Plain ArrayList Linq c# 2 syntaxs (need a conversion) 。

于 2013-10-19T19:26:09.027 回答
0

你会感谢这样的事情吗?

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"));
于 2013-10-19T19:15:40.497 回答