获取特定的属性,从列表中的所有项目 [英] Get specific property from all items from the list

查看:190
本文介绍了获取特定的属性,从列表中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的联系人列表:

 公共类联系
{
    私人字符串_FirstName;
    私人字符串_lastName;
    私人诠释_age;

    ///<总结>
    ///构造函数
    ///< /总结>
    ///< PARAM NAME =FNAME>联系人的姓名和LT; /参数>
    ///< PARAM NAME =L-NAME>联系人的姓氏和LT; /参数>
    ///< PARAM NAME =年龄与GT;联系人的年龄和LT; /参数>
    公共联系(字符串FNAME,LNAME字符串,INT岁)
    {
        _FirstName = FNAME;
        _lastName = LNAME;
        _age =年龄;
    }

    ///<总结>
    ///联系人姓
    ///< /总结>
    公共字符串名字
    {
        得到
        {
            返回_lastName;
        }
        组
        {
            _lastName =价值;
        }
    }

    ///<总结>
    ///联系人名字
    ///< /总结>
    公共字符串名字
    {
        得到
        {
           返回_FirstName;
        }
        组
        {
            _FirstName =价值;
        }
    }

    ///<总结>
    ///跟年龄
    ///< /总结>
    公众诠释年龄
    {
        得到
        {
            返回_age;
        }
        组
        {
            _age =价值;
        }
    }
}
 

在这里我创建我的名单:

 私人列表<联系与GT; _联系人列表;
_contactList =新的名单,其中,联系与GT;();
_contactList.Add(新联系人(约翰福音,禅师,45));
_contactList.Add(新联系人(杰克,李四,20));
_contactList.Add(新联系人(雅西,多尔,19));
_contactList.Add(新联系人(山姆,Josin,44));
 

现在我想获得单独的列表使用LINQ的所有联系人的所有名字。

到目前为止,我尝试:

 公开名单<字符串> FirstNames
    {
        得到
        {
           返回_contactList.Where(C => C.FirstName.ToList());
        }
    }
 

解决方案

您想要使用选择方法,而不是其中,这里:

  _contactList.Select(C => C.FirstName).ToList();
 

另外,需要对了ToList()只存在因为属性需要它。你可以返回一个的IEnumerable<字符串> 而不是,如果你想摆脱那种

I have list of Contacts:

public class Contact
{
    private string _firstName;
    private string _lastName;
    private int _age;

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="fname">Contact's First Name</param>
    /// <param name="lname">Contact's Last Name</param>
    /// <param name="age">Contact's Age</param>
    public Contact(string fname, string lname, int age)
    {
        _firstName = fname;
        _lastName = lname;
        _age = age;
    }

    /// <summary>
    /// Contact Last Name
    /// </summary>
    public string LastName
    {
        get
        {
            return _lastName;
        }
        set
        {
            _lastName = value;
        }
    }

    /// <summary>
    /// Contact First Name
    /// </summary>
    public string FirstName
    {
        get
        {
           return _firstName;
        }
        set
        {
            _firstName = value;
        }
    }

    /// <summary>
    /// Contact Age
    /// </summary>
    public int Age
    {
        get
        {
            return _age;
        }
        set
        {
            _age = value;
        }
    }
}

and here I am creating my list:

private List<Contact> _contactList;
_contactList = new List<Contact>();
_contactList.Add(new Contact("John", "Jackson", 45));
_contactList.Add(new Contact("Jack", "Doe", 20));
_contactList.Add(new Contact("Jassy", "Dol", 19));
_contactList.Add(new Contact("Sam", "Josin", 44));

Right now I am trying to get all the first names of all the contacts in separate list using LINQ.

So far I tried:

    public List<string> FirstNames
    {
        get
        {
           return _contactList.Where(C => C.FirstName.ToList());
        }
    }

解决方案

You want to use the Select method, not Where here:

_contactList.Select(C => C.FirstName).ToList();

Further, the need for the ToList() only exists because the property demands it. You could return an IEnumerable<string> instead if you wanted to get rid of that.

这篇关于获取特定的属性,从列表中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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