ASP.NET MVC에서는 기본적으로 생성된 프로젝트에서 모델(클래스)의 필드에 정의된 DisplayName을 View에서 표현할 수 있다. WPF/Winform에서 사용자 함수인 GetDisPlayName()을 사용해보고 여기에 LINQ, Lambda를 이용해서 모델을 사용하는 간단한 예제를 작성했다.
- GitHub : LINQ-Lambda Tutorial
public static string GetDisPlayName<T>(string fieldName) where T : class
{
var nameInfo = typeof(T).GetProperty(fieldName);
var obj = nameInfo?.GetCustomAttributes(typeof(DisplayNameAttribute), true);
return obj?.Cast<DisplayNameAttribute>().FirstOrDefault()?.DisplayName ?? fieldName;
}
// Console.WriteLine(GetDisPlayName<Person>(nameof(Person.EmpName)));
화면에서 사용한 전체 소소는 아래의 내용을 참고한다.
모델( class)
public class TEmployee
{
[DisplayName("사원번호")]
public int EmpNO { get; set; }
[DisplayName("사원명")]
public string EmpName { get; set; }
[DisplayName("연봉")]
public int Salary { get; set; }
public TDepartment Department { get; set; }
}
public class TDepartment
{
[DisplayName("부서번호")]
public int DeptNO { get; set; }
[DisplayName("부서명")]
public string DeptName { get; set; }
public virtual IEnumerable<TEmployee> Employee { get; set; }
}
public class TEmpDept
{
public IList<TEmployee> Employee { get; set; }
public IList<TDepartment> Department { get; set; }
}
전체소스
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
private static void Main()
{
var emp = new List<TEmployee>
{
new TEmployee {EmpNO = 101, EmpName = "홍길동", Department = new TDepartment {DeptNO = 201}},
new TEmployee {EmpNO = 102, EmpName = "홍길서", Department = new TDepartment {DeptNO = 202}},
new TEmployee {EmpNO = 103, EmpName = "홍길남", Department = new TDepartment {DeptNO = 202}},
new TEmployee {EmpNO = 104, EmpName = "홍길북", Department = new TDepartment {DeptNO = 203}}
};
var dept = new List<TDepartment>
{
new TDepartment {DeptNO = 201, DeptName = "부서1", Employee = emp.Where(x => x.Department.DeptNO == 201)},
new TDepartment {DeptNO = 202, DeptName = "부서2", Employee = emp.Where(x => x.Department.DeptNO == 202)},
new TDepartment {DeptNO = 203, DeptName = "부서3", Employee = emp.Where(x => x.Department.DeptNO == 203)}
};
foreach (var d in dept)
{
Console.WriteLine($"{d.DeptNO} {d.DeptName}");
d.Employee.ToList().ForEach(e => Console.WriteLine($" {e.EmpNO} {e.EmpName}"));
}
Console.WriteLine("---------------------------------------");
foreach (var d in dept)
{
Console.WriteLine($"{d.DeptNO} {d.DeptName}");
foreach (var e in emp.Where(e => e.Department.DeptNO == d.DeptNO))
{
Console.WriteLine($" {e.EmpNO} {e.EmpName}");
}
}
Console.WriteLine("---------------------------------------");
/*
var query =
from d in dept
join e in emp
on d.DeptNO equals e.Department.DeptNO
select new
{
d.DeptNO,
d.DeptName,
e.EmpNO,
e.EmpName
};
*/
var query = dept.Join(emp, d => d.DeptNO, e => e.Department.DeptNO, (d, e) => new {d.DeptNO, d.DeptName, e.EmpNO, e.EmpName});
foreach (var item in query)
{
Console.WriteLine($"{item.DeptNO}, {item.DeptName}, {item.EmpNO}, {item.EmpName}");
}
Console.WriteLine("-------------------------------------------------------");
var empDept = new List<TEmpDept>
{
new TEmpDept {Department = dept, Employee = emp}
};
foreach (var item in empDept.Where(item => item != null))
{
item.Employee.ToList().ForEach(e =>
Console.WriteLine($"{GetDisPlayName<TDepartment>("DeptNO")}:{e.Department.DeptNO}, " +
$"{GetDisPlayName<TDepartment>("DeptName")}:{item.Department.FirstOrDefault(d => d.DeptNO == e.Department.DeptNO).DeptName}, " +
$"{GetDisPlayName<TEmployee>("EmpNO")}:{e.EmpNO}, " +
$"{GetDisPlayName<TEmployee>("EmpName")}:{e.EmpName}")
);
}
Console.WriteLine("-------------------------------------------------------");
}