반응형
C # : 개체 목록을 해당 개체의 단일 속성 목록으로 변환하는 방법은 무엇입니까?
내가 가지고 있다고 :
IList<Person> people = new List<Person>();
그리고 person 개체에는 FirstName, LastName 및 Gender와 같은 속성이 있습니다.
이것을 Person 객체의 속성 목록으로 어떻게 변환 할 수 있습니까? 예를 들어, 이름 목록에.
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();
반응형
'developer tip' 카테고리의 다른 글
Scala에서 두 개 이상의 목록을 함께 압축 할 수 있습니까? (0) | 2020.09.15 |
---|---|
글꼴 크기 변경 단축키 (0) | 2020.09.15 |
Sphinx의 autodoc을 사용하여 클래스의 __init __ (self) 메서드를 문서화하는 방법은 무엇입니까? (0) | 2020.09.14 |
Eclipse에 포함 된 외부 라이브러리로 jar를 만드는 방법은 무엇입니까? (0) | 2020.09.14 |
Swift에서 뷰 컨트롤러와 다른 객체간에 데이터를 어떻게 공유합니까? (0) | 2020.09.14 |