从一个ArrayList中检索对象具有特定元素值 [英] Retrieve object from an arraylist with a specific element value

查看:138
本文介绍了从一个ArrayList中检索对象具有特定元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些属性的类。例如类人有姓名,年龄,性别等作为属性。

人们填写一份表格,以除其他外他们的性别是一个DropDownList,他们提交该人被添加到ArrayList

我需要的是一个函数通过点击一个按钮来显示所有谁选择女性性别的人。​​

有人可以帮帮我吗?我曾尝试,现在,现在开始有点desperat搜索了好几天了正确的答案。

非常感谢!

奥拉夫

这是我的.cs code

 公共类Person
{
    私人字符串名称;
    私人字符串ARTISTNAME;
    私人字符串的地址;
    私人双号;
    私人双拉链;
    私人串日;
    私人字符串性别;公众人物(字符串名称,字符串ARTISTNAME,字符串地址,双号,双拉链,串天,串性别)
    {
        this.name =名称;
        this.artistname = ARTISTNAME;
        this.address =地址;
        this.number =号;
        this.zip =拉链;
        this.day =天;
        this.gender =性别;
    }    公共重写字符串的ToString()
    {
        字符串newPerson =名+又名+ ARTISTNAME +住在+地址++数字++拉链++日+性别:+性别;
        返回newPerson;
    }
}

这是我的.aspx code:

 公共部分类指数:System.Web.UI.Page
{
    静态的ArrayList personArrayList;    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!Page.IsPostBack)
        {
            personArrayList =新的ArrayList();
        }
    }    保护无效btnSubmit_Click(对象发件人,EventArgs的发送)
    {
        人p =新的Person(txtName.Text,txtArtistName.Text,txtAddress.Text,Convert.ToDouble(txtNumber.Text),Convert.ToDouble(txtPostal.Text),Convert.ToString(dropdownDay.Text),Convert.ToString(dropdownGender 。文本));
        personArrayList.Add(P);
    }    保护无效btnShowAll_Click(对象发件人,EventArgs的发送)
    {
        ListBoxShow.Items.Clear();
        的for(int i = 0; I< personArrayList.Count;我++)
        {
            ListBoxShow.Items.Add(personArrayList [I]的ToString());
        }
    }    保护无效btnShowGentle_Click(对象发件人,EventArgs的发送)
    {
        ListBoxShow.Items.Clear();    }    保护无效btnShowLadies_Click(对象发件人,EventArgs的发送)
    {
        ListBoxShow.Items.Clear();
        的for(int i = 0; I< personArrayList.Count;我++)
        {
            如果(personArrayList [I] .gender =女)
            {            }
        }
    }
}


解决方案

我觉得这样的事情应该做的伎俩

 使用System.Linq的;
VAR女=从Person p在personArrayList那里P.Gender ==女选择P;


我有这个有些问题自己,所以问了一个问题<一href=\"http://stackoverflow.com/questions/19470731/plain-arraylist-linq-c-sharp-2-syntaxes-need-a-conversion/19470765?noredirect=1#19470744\">Plain ArrayList的LINQ的C#语法2(需要转换),可以对你有用。

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

            }
        }
    }
}

解决方案

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.

这篇关于从一个ArrayList中检索对象具有特定元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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