147

Say I have:

IList<Person> people = new List<Person>();

And the person object has properties like FirstName, LastName, and Gender.

How can I convert this to a list of properties of the Person object. For example, to a list of first names.

IList<string> firstNames = ???

5 Answers 5

241
List<string> firstNames = people.Select(person => person.FirstName).ToList();

And with sorting

List<string> orderedNames = people.Select(person => person.FirstName).OrderBy(name => name).ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Also, how would I sort that alphabetically by firstname?
List<string> firstNames = people.Select(person => person.FirstName).ToList().Sort(); This will sort using the default alphabetic sorting of string.
Sort() doesn't support a fluent interface! Call firstNames.Sort() separately
var list = from person in people orderby person.FirstName select person.FirstName;
8
IList<string> firstNames = (from person in people select person.FirstName).ToList();

Or

IList<string> firstNames = people.Select(person => person.FirstName).ToList();

Comments

4
firstNames = (from p in people select p=>p.firstName).ToList();

2 Comments

Using a query expression in this case is overkill, IMO. Dot notation has less fluff if you've just got one simple operation.
True, but the question was "How can this be done" ... not "How can this be done with the least amount of fluff". No disrespect intended, Jon. (Please don't smite me).
2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestProject
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SampleDataContext context = new SampleDataContext();
            List<Employee> l = new List<Employee>();
            var qry = from a in context.tbl_employees where a.Gender=="Female"  
                orderby  a.Salary ascending
            select new Employee() {
                           ID=a.Id,
                           Fname=a.FName,
                           Lname=a.Lname,
                           Gender=a.Gender,
                           Salary=a.Salary,
                           DepartmentId=a.DeparmentId
            };
            l= qry.ToList();
            var e1 =  from  emp in context.tbl_employees
                where emp.Gender == "Male"
                orderby emp.Salary descending
                select  emp;
            GridView1.DataSource = l;
            GridView1.DataBind();
        }
    }
    public class Employee
    {
        public Int64 ID { get; set; }
        public String Fname { get; set; }
        public String Lname { get; set; }
        public String Gender { get; set; }
        public decimal? Salary { get; set; }
        public int? DepartmentId { get; set; }
    }
}

Comments

2
using System.Collections.Generic;
using System.Linq;

IList<Person> people = new List<Person>();
IList<string> firstNames = people.Select(person => person.FirstName).ToList();

1 Comment

Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made

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.