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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1461072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:47:28  来源:igfitidea点击:

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

c#linqlist

提问by User

Say I have:

说我有:

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

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

person 对象具有 FirstName、LastName 和 Gender 等属性。

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

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

IList<string> firstNames = ???

采纳答案by Dario

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

And with sorting

并与排序

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

回答by Gregtheitroade

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

回答by Jon Sagara

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

Or

或者

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

回答by Mohd Shahnawaz

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

回答by M Fatih Koca

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

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