/// <summary> ///tell list set to convert to DataTable/// </summary> /// <param name= "list" ></param> /// <returns></returns> Public StaticSystem.Data.DataTable listtodatatable (IList list) {System.Data.DataTable result=NewSystem.Data.DataTable (); if(list. Count >0) {propertyinfo[] propertys= list[0]. GetType (). GetProperties (); foreach(PropertyInfo Piinchpropertys) { //Get TypeType Coltype =Pi. PropertyType; //When the type is nullable<> if((Coltype.isgenerictype) && (coltype.getgenerictypedefinition () = =typeof(nullable<>)) {Coltype= Coltype.getgenericarguments () [0]; } result. Columns.Add (pi. Name, Coltype); } for(inti =0; I < list. Count; i++) {ArrayList templist=NewArrayList (); foreach(PropertyInfo Piinchpropertys) { Objectobj = pi. GetValue (List[i],NULL); Templist.add (obj); } Object[] Array =Templist.toarray (); Result. Loaddatarow (Array,true); } } returnresult; }
C#_list Convert to DataTable