Generic import, validation, and exception hints

Source: Internet
Author: User

Data validation
Validata<t> ();
Convert to Collection
datatabletolist<t>();
Data exception Handling
setvalue<t>();
File Import
Excelimport();



1PublicClassGenericexceltolist2{3PublicStatic list<validationresult> validata<t> (list<t>Lists) {4var VRS =New list<validationresult>();5for (int i =0; I < lists. Count; i++) {6 list<validationresult> VR =Validata (Lists[i]);7foreach (var VDInchVR) {8 VD. ErrorMessage ="The"+ (i +1) +"Yes" +Vd. errormessage;9}10Vrs. AddRange (VR);11if (VR. Count >3)12{VRS = Vrs. GetRange (0,3);Vrs[vrs. Count-1]. ErrorMessage + ="etc...";15Break;16}17}18ReturnVrs19}20PublicStatic list<validationresult> validata<t>(T T)21st{22var VC =New Validationcontext (T,NullNull);23var VR =New list<validationresult>();24Validator.tryvalidateobject (T, VC, VR, trure);25ReturnVr26}27PublicStaticvoid Setvalue<t> (PropertyInfo property,StringValue, T t) {28String type =Property. PropertyType.Name.ToLower ();29if (type = ="Nullable ' 1")Type = property. Propertytype.getgenericarguments () [0]. Name.tolower ();31Switch(type) {32Case"Int32":The property. SetValue (T,convert.toint32 (value),Null);34Break;35Case"String":. SetValue (T,value,Null);37Break;38Case"Double":The property. SetValue (t, convert.todouble (value),Null);40Break;41Case"Decimal":. SetValue (t, Convert.todecimal (value),Null);43Break;44Case"Datetime":. SetValue (t, Convert.todatetime (value),Null);46Break;47Default:The property. SetValue (t, value,Null);49Break;50}51}52PublicStaticvoid Datatabletolist<t> (string keys, list<t> lists, DataTable DT)where T:New() {53string[] Splitkeys = keys. Split (‘,‘);Propertyinfo[] Properties =typeof (T). GetProperties (BindingFlags.Public |BindingFlags.Instance);55for (var row =0; Row < dt. Rows.Count; row++) {56var t =NewT ();57for (var column =0; Column < dt. Columns.count; column++) {58Try59{60var value =Dt. Rows[row][column]. ToString ();61if (value = ="")Continue;62if (Column > Splitkeys.count ())Break;PropertyInfo Property = Properties. FirstOrDefault (m = M.name = =Splitkeys[column]);64if (property = =NullContinue;65SetValue (property, value, T);66}67Catch(Exception e)68{e.helplink="Import Data section"+ (row+1) +"Yes"+dt. Columns[column]. columnname+"Data format is wrong!";70ThrowE71}72}73Lists. ADD (t);74}75}76PublicStatic Boolean iscolumnnamesequal (StringKeys, DataTable DT)77{78var columnnames = (From DataColumn columnIn dt. ColumnsSelectColumn. ColumnName). ToList ();79String dtcolumnnames =String. Join (",", ColumnNames);80return keys = =Dtcolumnnames;81}82PublicStatic Boolean iscolumnsequal (StringKeys, DataTable dt) {83string[] Splitkeys = keys. Split (‘,‘);84return Splitkeys.count () = =Dt. Columns.count;85}86PublicStatic DataTable Excelimport (StringFileName) {87var book =new Workbook (fileName); Worksheet sheet = Book. worksheets[0]; The cells cells = sheet. Cells; The DataTable dt = cells. Exportdatatableasstring (0, 0, cells. Maxdatarow + 1, cells. Maxdatacolomn + 1, true); The return of the DT; ()

Generic import, validation, and exception hints

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.