아래의 유틸리티 기능 (데이터 테이블에 나열)에서 목록의 일부 멤버를 제외 할 수 있습니까?데이터 테이블을 만들기 위해 일부 목록 속성을 제외하는 방법
처럼, 나는 속성을 만들고, 아래의 유틸리티 메소드에
public class PointDataClone
{
public int DataId { get; set; }
public string UniqueKey { get; set; }
public int Count { get; set; }
public List<PointToPointData> PointToPointData { get; set; }
}
유틸리티 기능,
public static DataTable ToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dataTable.Columns.Add(propertyDescriptor.Name, type);
}
object[] values = new object[propertyDescriptorCollection.Count];
foreach (T iListItem in iList)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
당신은 루프에 대한 귀하의 클래스에 데이터 주석을 사용하여 사람들을 테스트 할 수 당신이 이러한 주석이 특성을 발생할 때 계속 수 있습니다. –
우수 아이디어, 모든 샘플 코드 – user584018
사용하는 경우 : dataTable.Rows.Add (values); 배열 'values'는 데이터 테이블의 열과 동일한 순서 여야합니다. 데이터 테이블에 8 개의 열이 포함 된 경우 값에 데이터 테이블의 처음 5 개 열이 포함될 수 있습니다. 데이터 테이블의 마지막 3 열은 null로 채워집니다. 때로는 필드 유형이 널을 허용하지 않으면 널 (null)로 오류를 얻을 수 있습니다. UniqueKey 및/또는 PointToPointData가 배열 중간에 있으면 코드에 문제가 있습니다. 일반적으로 다음과 같은 dataTable.Rows.Add (새 개체 [] {input1, input2, input5, input6, input10}); – jdweng