2012-10-17 3 views
0
public static List<T> DataTable2List(DataTable dt, int index) 
{ 
      List<T> lst = new List<T>(); 
      lst = (from row in dt.AsEnumerable() select Convert.ChangeType(row[0], typeof(T))).ToList(); 
      return lst; 
} 

오류 1 암시 적으로 유형 'System.Collections.Generic.List을'변환 할 수 없습니다 'System.Collections.Generic.List'오류를 제거하는 방법DataTable의 배선 일반적인 방법을 목록으로

. 그리고 나는 함수의 일반성을 원하지 않는다.

+0

이 방법을 어떻게 사용 하시겠습니까? –

답변

1

public static List<T> DataTable2List<T>(DataTable dt, int index) where T : IConvertible 
{ 
    List<T> lst = new List<T>(); 
    lst = (from row in dt.AsEnumerable() select (T)Convert.ChangeType(row[0], typeof(T))).ToList(); 
    return lst; 
} 

시도 그리고 함수의 genericness을 돼요.

허? generics를 사용하지 않고 generic 타입 인 T을 사용하고 싶습니까? 안돼.

+0

잠재적으로'row [0] .GetType()'을 사용할 수 있습니까? 그냥 생각. – LukeHennerley

+0

업데이트됩니다. 하지만 너의 제안은 효과가있다. – NSN