C#: 如何将对象列表转换为该对象的单个属性列表?

141

假设我有以下代码:

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

而且person对象具有类似于FirstName、LastName和Gender的属性。

我该如何将其转换为Person对象属性的列表。例如,转换为名字列表。

IList<string> firstNames = ???
6个回答

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

并且带有排序功能

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

谢谢。另外,我该如何按名字的字母顺序对其进行排序? - User
List<string> firstNames = people.Select(person => person.FirstName).ToList().Sort(); 这将使用字符串的默认字母排序对其进行排序。 - Paul Williams
Sort()不支持流畅接口!请单独调用firstNames.Sort()。 - Dario
变量列表 = 从人中选择 person.FirstName 按 person.FirstName 排序; - ConsultUtah

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

或者

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

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

8
在我看来,对于这种情况使用查询表达式有点过头了。如果只需要一个简单的操作,使用点符号表示法更加简洁明了。 - Jon Skeet
2
没错,但问题是“如何做到这一点”……而不是“如何用最少的废话做到这一点”。请原谅我有任何不尊重之处,Jon。(请不要惩罚我)。 - Dan Esparza

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

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

感谢您提供这段代码片段,它可能会在短期内提供一些有限的帮助。通过展示为什么这是一个好的解决方案,适当的解释将极大地提高其长期价值,并使其对未来读者具有其他类似问题更有用。请编辑您的答案以添加一些解释,包括您所做出的假设。 - Shawn C.

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

-1

这将把它转换为列表:

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

这将返回一个(第一个):

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

问题已经得到回答,请勿抄袭他人答案。 - CrudaLilium

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接