에서 중첩 된 모델을 만들어 데이터베이스에서 데이터를 가져 오는에 대한 기존의 클래스를목록
public class Employee
{
public int? EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int? LocationId { get; set; }
public string LocationName { get; set; }
public int? DesignationId { get; set; }
public string DesignationName { get; set; }
}
코드가 :
using (var ds = new EmployeeDS())
{
using (var dataAdapter = new DataSet.EmployeeDSTableAdapters.EmployeeTableAdapter())
{
dataAdapter.FillEmployee(ds.Employee,Id);
var result = (from DataRow row in ds.Employee.Rows
select new Employee
{
EmployeeId = (row["EmployeeID"] == DBNull.Value) ? null : (int?)row["EmployeeID"],
EmployeeName = (row["EmployeeName"] == DBNull.Value) ? string.Empty : (string)row["EmployeeName"],
LocationId = (row["LocationId"] == DBNull.Value) ? null : (int?)row["LocationId"],
LocationName = (row["LocationName"] == DBNull.Value) ? string.Empty : (string)row["LocationName"],
DesignationId = (row["DesignationId"] == DBNull.Value) ? null : (int?)row["DesignationId"],
DesignationName = (row["DesignationName"] == DBNull.Value) ? string.Empty : (string)row["DesignationName"],
}).ToList();
}
}
그것의 여러 위치 또는을 가진 직원이 반환 여러 행 됐었 작업 내가 USI 오전
public class EmployeeNested
{
public int? EmployeeId { get; set; }
public string EmployeeName { get; set; }
public List<Location> Locations { get; set; }
public List<Designation> Designations { get; set; }
}
public class Location
{
public int? LocationId { get; set; }
public string LocationName { get; set; }
}
public class Designation
{
public int? DesignationId { get; set; }
public string DesignationName { get; set; }
}
: designation.So 내가 좋아하는 중첩 된 모델로 데이터를 반환해야 NG 중첩 된 모델을 반환 코드 :
var tempList=result.GroupBy(x => x.EmployeeId, (key, g) => g.OrderBy(e => e.EmployeeId).First());
foreach (var item in tempList)
{
item.Designations = result
.Where(x => x.EmployeeId== item.EmployeeId)
.Select(x => new Designation
{
DesignationId = x.DesignationId,
DesignationName = x.DesignationName
}).ToList();
item.Locations = result
.Where(x => x.EmployeeId== item.EmployeeId)
.Select(x => new Location
{
LocationId = x.LocationId,
LocationName = x.LocationName
}).ToList();
}
질문 :
중첩 된 목록에 단순 목록을 변환 할 수있는 더 나은 솔루션이 있습니까?
플랫 목록을 중첩 목록으로 변환하기위한 일반적인 방법을 만들 수 있습니까? 그래서 나는 다른 기능을 위해 그것을 재사용 할 수있다.
데이터 세트에서 직접 중첩 목록을 만들 수 있습니까?
나는이 일을하기에 좋은 단색 패턴이 있다고 확신한다. 나는 그것을 찾을 수 없다.
테이블 간에는 아무런 관련이 없습니다. 또한 프로젝트 전체에서 데이터 세트를 사용했습니다. 그래서 내가 한 일은 중첩 목록을 만드는 올바른 방법일까요? – Binu