C#: How to convert a list of objects to a list of a single property of that object?

C#LinqList

C# Problem Overview


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 = ???

C# Solutions


Solution 1 - C#

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

And with sorting

List<string> orderedNames = people.Select(person => person.FirstName).OrderBy(name => name).ToList();

Solution 2 - C#

IList<string> firstNames = (from person in people select person.FirstName).ToList();

Or

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

Solution 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

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

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

Solution 6 - C#

This will turn it to a list:

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

This will return one (the first one):

var firstname = people.select(e => e.firstname).FirstOrDefault();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUserView Question on Stackoverflow
Solution 1 - C#DarioView Answer on Stackoverflow
Solution 2 - C#Jon SagaraView Answer on Stackoverflow
Solution 3 - C#GregoireView Answer on Stackoverflow
Solution 4 - C#Mohd ShahnawazView Answer on Stackoverflow
Solution 5 - C#M Fatih KocaView Answer on Stackoverflow
Solution 6 - C#Tayo OyetunjiView Answer on Stackoverflow