Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:如何将对象列表转换为该对象的单个属性列表?_C#_Linq_List - Fatal编程技术网

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

C# C:如何将对象列表转换为该对象的单个属性列表?,c#,linq,list,C#,Linq,List,假设我有: IList<Person> people = new List<Person>(); person对象具有FirstName、LastName和Gender等属性 如何将其转换为Person对象的属性列表。例如,一个名字列表 IList<string> firstNames = ??? 和排序 List<string> orderedNames = people.Select(person => person.FirstNam

假设我有:

IList<Person> people = new List<Person>();
person对象具有FirstName、LastName和Gender等属性

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

IList<string> firstNames = ???
和排序

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


这将把它变成一个列表:

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

在这种情况下使用查询表达式是矫枉过正的,依我看。如果你只做了一个简单的操作,点表示法就不会有那么多问题。没错,但问题是如何做到这一点。。。这不是用最少的绒毛就能做到的。没有不尊重的意思,乔恩。请不要打我,谢谢。另外,我应该如何按名字的字母顺序排序?列出名字=人。Selectperson=>person.firstname.ToList.sort;这将使用默认的字符串字母排序。排序不支持流畅的界面!Call firstNames.Sort separatelyvar list=根据person.FirstName选择person.FirstName;感谢您提供此代码片段,它可能会提供一些有限的短期帮助。通过说明为什么这是一个很好的解决问题的方法,并向未来有其他类似问题的读者解释它的长期价值。请编辑您的答案以添加一些解释,包括您所做的假设。问题已经得到回答,请勿复制其他人的答案。
List<string> orderedNames = people.Select(person => person.FirstName).OrderBy(name => name).ToList();
IList<string> firstNames = (from person in people select person.FirstName).ToList();
IList<string> firstNames = people.Select(person => person.FirstName).ToList();
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; }
    }
}
using System.Collections.Generic;
using System.Linq;

IList<Person> people = new List<Person>();
IList<string> firstNames = people.Select(person => person.FirstName).ToList();
List<string> firstNames = people.Select(person => person.FirstName).ToList();
var firstname = people.select(e => e.firstname).FirstOrDefault();