Generic type additions and deletions change

Source: Internet
Author: User
 #region Insert operation 2///<summary> 3///insert operation 4///</summary> 5///<param name= "Person" >&lt ;/param> 6///<returns></returns> 7 public void Insert (T t) 8 {9 Usin                     G (Mongo Mongo = new Mongo (configuration)) {One try 12 {13 Mongo.
 Connect (); var db = Mongo.
 Getdatabase (DatabaseName); var collection = db.
 Getcollection<t> (CollectionName); Collection.
 Insert (T, true); Mongo.
 Disconnect (); catch (Exception) {MONGO.
 Disconnect ();
 throw; #endregion #region Update 34 (///) <summary> 35///Update operation///</summary> 37 <param name= "Person" ></param>///<returns></returns> "T" T,
 Expression<func<t, bool>> Func) {(Mongo Mongo = new Mongo (configuration)) {try MONGO.
 Connect (); var db = Mongo.
 Getdatabase (DatabaseName); var collection = db.
 Getcollection<t> (CollectionName); Wuyi collection.
 Update<t> (T, Func, True); Mongo.
 Disconnect (); The catch (Exception) is {MONGO.
 Disconnect ();
 throw; The #endregion #region get a collection of 66/// <summary> 67///Acquisition Collection///</summary>///<param name= "Person" ></param>///<returns></returns> list<t> List (int pageIndex, int pagesiz
 E, expression<func<t, bool>> Func, out int pagecount) (PageCount = 0;                 Mongo using (Mongo = new Mongo (configuration)) {MONGO.
 Connect (); BA bayi var db = Mongo.
 Getdatabase (DatabaseName); Collection var = db.
 Getcollection<t> (CollectionName); PageCount = Convert.ToInt32 (collection.
 Count ()); The var personlist = collection. Linq (). Where (func). Skip (PageSize * (pageIndex-1)) 88. Take (pageSize). Select (i => i).
 ToList (); Mongo.
 Disconnect ();
 Personlist return; CatcH (Exception) (MONGO).
 Disconnect ();
 throw; 102 #endregion #region read a single record 105// /<summary> 106///Read single record///</summary> 108///<param name= "person" ></param> 109///<ret             urns></returns> expression<func<t Public T single (bool>> Func) 111 {112                     using (Mongo Mongo = new Mongo (configuration)) 113 {114 Try 115 {116 Mongo.
Connect (); 117 118 var db = Mongo.
Getdatabase (DatabaseName); 119 var collection = db.
Getcollection<t> (CollectionName); 121 122 var single = collection. Linq ().
FirstOrDefault (func); 123 124 MONGO.
Disconnect ();
126 return single; 127 128} 129                 catch (Exception) 130 {131 MONGO.
Disconnect ();
132 throw;  134} 135} 136 #endregion 137 138 #region Delete operation 139/// <summary> 140///Delete operation///</summary>///<param name= "Person" ></param> 143///<retur             ns></returns> 144 public void Delete (expression<func<t, bool>> Func) 145 {146                     using (Mongo Mongo = new Mongo (configuration)) 147 {148 Try 149 {150 Mongo.
Connect (); 151 152 var db = Mongo.
Getdatabase (DatabaseName); 153 154 var collection = db.
Getcollection<t> (CollectionName); 155 156//This place should note that the T parameter must be added, otherwise it will be treated as Object type 157//Cause deletion failure 158 collection.
Remove<t> (func); 159 160 MONGO.
Disconnect ();161 162} 163 catch (Exception) 164 {165 MONGO.
Disconnect ();
166 throw; 167} 169} 170 #endregion

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.