34

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());
        }
    }
2
  • 3
    Just a note for your accessors in your class, you can simply type public int Age {get; set; } all on one line instead of what you have since you're not performing any other actions in the accessor. It's, obviously, much shorter and easier to read. Commented Nov 13, 2013 at 19:45
  • 1
    Where is for evaluating a Select condition like: foreach contact where (firstname starts with "J") select (contact) Commented Nov 13, 2013 at 19:45

2 Answers 2

58

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Stanislav, I'm glad I could be of assistance!
What if FirstName is dynamic, may come from parameter as string?
@ibubi if FirstName is dynamic then you'd want to cast that inside the Select expression.
7
public List<string> FirstNames
{
    get
    {
       return _contactList.Select(C => C.FirstName).ToList();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.