C # learns from the basics string. Join (",", Keys.) ToArray ())

Source: Internet
Author: User

I always feel that I have been working for 6 years and experienced. It wasn't until recently that a. NET Advanced class was reported. I'm still a long way off. Take String.Join contrast.

My Code:

 Public Static intInsertmodel<t> (T t)whereT:basemodel {Type type=typeof(T); stringColumnstrings =string. Join (",", type. GetProperties (). Select (p =string. Format ("[{0}]", P.name))); stringsql ="Insert into"+type. name+"("; foreach(varIteminchtype. GetProperties ()) {if(item. Name! ="Id") {SQL+=item. name+","; }} SQL= SQL. Substring (0Sql. Length-1); SQL+=")"; SQL+="VALUES ("; foreach(varIteminchtype. GetProperties ()) {if(item. Name! ="Id") {SQL+="'"+ Item. GetValue (t) +"',"; }} SQL= SQL. Substring (0Sql. Length-1); SQL+=")"; returndbhelpersql.executesql (SQL); }

Will find in the insert when I splicing more hard, and then the excellent work of our class compared a bit. Here's his code:

  Public BOOLInsert<t> (T t)whereT:basemodel {//when inserting an entity, it is necessary to determine the time of insertion. You can also determine the length of the data by its characteristics.//The contract ID is self-growing.            varType =typeof(T); varKeys =Newlist<string>(); varValues =Newlist<string>(); varPar =NewList<sqlparameter>(); foreach(varIteminchtype. GetProperties ()) {varValue =item.                GetValue (t); if(item. Name.tolower () = ="ID")Continue; Keys. ADD (item.                Name); Values. ADD ("@"+item.                Name); Par. ADD (NewSqlParameter ("@"+item.            Name, value)); }            varc =string. Join (",", Keys.            ToArray ()); varv =string. Join (",", values.            ToArray ()); varsql =string. Format ($"INSERT into [{type. Name}] ({c})"+                $"VALUES ({v})"); returnexcutesql<BOOL> (sql, cmd = =            {                foreach(varParameterinchpar) cmd.                Parameters.Add (parameter); varresult =cmd.                ExecuteNonQuery (); returnResult >0;            }); //return runcmd (SQL, par. ToArray ());}

He was found to have used string.join skillfully. I saved a lot of code compared to mine.

Later when the encourage each other, the code more on a layer!!!

C # learns from the basics string. Join (",", Keys.) ToArray ())

Related Article

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.