简体   繁体   English

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

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

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.如何将其转换为 Person 对象的属性列表。 For example, to a list of first names.例如,到一个名字列表。

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

并与排序

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();
firstNames = (from p in people select p=>p.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();

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();

如何转换列表<object>至 Json | C#<div id="text_translate"><p> 我有一个由对象组成的列表,每个 object 有 5 个数据。 我需要将该列表转换为 json,但使用序列化它会填满空的 json。</p><p> 有谁知道我可能做错了什么?</p><pre> foreach (DataRow dtRow in dtAlarmas.Rows) { String Name = dtRow["Name"].ToString(); String ID = dtRow["ID"].ToString(); String AlarmText = dtRow["AlarmText"].ToString(); String AlarmTimeNoNula = dtRow["AlarmTimeNoNula"].ToString(); lstAlarmasNoTratadas.Add(new Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel(dtRow["Name"].ToString(), Convert.ToInt32(dtRow["ID"]), dtRow["Class"].ToString(), dtRow["AlarmText"].ToString(), dtRow["AlarmTimeNoNula"].ToString())); } string sParams = JsonConvert.SerializeObject(lstAlarmasNoTratadas);</pre><p> 转换后的 sParams 值 = "[{}]"</p><p> Class Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel</p><pre> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ondoan.DatosAux.Alarmas { public class AlarmaNoTratadaModel { private string Name; private int ID; private string Class; private string AlarmText; private string AlarmaTimeNoNula; public AlarmaNoTratadaModel(string Name, int ID, string Class, string AlarmText, string AlarmaTimeNoNula) { // TODO: Complete member initialization this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula; } public class AlarmaNoTratadasModel { public AlarmaNoTratadasModel() { } public AlarmaNoTratadasModel(String Name, Nullable&lt;System.Int32&gt; ID, String Class, String AlarmText, String AlarmaTimeNoNula) { this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula.ToString(); } public System.String Name { get; set; } public Nullable&lt;System.Int32&gt; ID { get; set; } public System.String Class { get; set; } public System.String AlarmText { get; set; } public System.String AlarmaTimeNoNula { get; set; } } } }</pre></div></object> - How to Convert List<Object> to Json | C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过列表属性C#将对象拆分为对象列表 - How to split object into list of objects by list property C# c#按对象的属性对对象列表进行排序 - c# Sort a list of objects by a property of an object 插入到使用对象属性进行排序的对象的C#列表中 - Inserting into a C# List of Objects that uses a Property of the Object to Sort C# - 按文本为每个对象属性过滤对象列表 - C# - Filter a list of objects by text for each object property 通过C#中另一个对象的属性对对象列表进行排序 - Ordering a list of objects by another object's property in C# 如何转换列表<object>至 Json | C#<div id="text_translate"><p> 我有一个由对象组成的列表,每个 object 有 5 个数据。 我需要将该列表转换为 json,但使用序列化它会填满空的 json。</p><p> 有谁知道我可能做错了什么?</p><pre> foreach (DataRow dtRow in dtAlarmas.Rows) { String Name = dtRow["Name"].ToString(); String ID = dtRow["ID"].ToString(); String AlarmText = dtRow["AlarmText"].ToString(); String AlarmTimeNoNula = dtRow["AlarmTimeNoNula"].ToString(); lstAlarmasNoTratadas.Add(new Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel(dtRow["Name"].ToString(), Convert.ToInt32(dtRow["ID"]), dtRow["Class"].ToString(), dtRow["AlarmText"].ToString(), dtRow["AlarmTimeNoNula"].ToString())); } string sParams = JsonConvert.SerializeObject(lstAlarmasNoTratadas);</pre><p> 转换后的 sParams 值 = "[{}]"</p><p> Class Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel</p><pre> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ondoan.DatosAux.Alarmas { public class AlarmaNoTratadaModel { private string Name; private int ID; private string Class; private string AlarmText; private string AlarmaTimeNoNula; public AlarmaNoTratadaModel(string Name, int ID, string Class, string AlarmText, string AlarmaTimeNoNula) { // TODO: Complete member initialization this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula; } public class AlarmaNoTratadasModel { public AlarmaNoTratadasModel() { } public AlarmaNoTratadasModel(String Name, Nullable&lt;System.Int32&gt; ID, String Class, String AlarmText, String AlarmaTimeNoNula) { this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula.ToString(); } public System.String Name { get; set; } public Nullable&lt;System.Int32&gt; ID { get; set; } public System.String Class { get; set; } public System.String AlarmText { get; set; } public System.String AlarmaTimeNoNula { get; set; } } } }</pre></div></object> - How to Convert List<Object> to Json | C# 如何将对象转换为列表 <string> 在c#? - How to convert Object to List<string> in c#? C# - 将对象列表转换为多个对象属性的数组 - C# - convert a list of objects to an array of multiple of the object's properties 将 C# 对象列表转换为 JavaScript 对象数组 - Convert C# List of object to JavaScript array of objects 如何在 C# 中将对象列表转换为对象元组 - How to convert List of objects to Tuple of objects in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM