Funny LINQ Part2: Create a generic operation class for the LINQ database (below)

Source: Internet
Author: User

Funny LINQ Part2: Create a generic operation class for the LINQ database (below)

The previous article introduced how to implement a general insert operation, that is, directly using utility. insert (XXX. Then, you may naturally think about whether you can implement General update, delete, and query operations. The answer is yes!

Next let's extend the Dal mentioned in the previous article:

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace dal
  6. {
  7. Public class Utility
  8. {
  9. Public static void insert <t> (T tentity) where T: Class
  10. {
  11. VaR table = tablefactory. createtable <t> ();
  12. Table. insertonsubmit (tentity );
  13. }
  14. Public static void Delete <t> (T tentity) where T: Class
  15. {
  16. VaR table = tablefactory. createtable <t> ();
  17. Table. deleteonsubmit (tentity );
  18. }
  19. Public static void Delete <t> (func <t, bool> predicate) where T: Class
  20. {
  21. VaR table = tablefactory. createtable <t> ();
  22. VaR dresult = where <t> (predicate );
  23. Table. deleteallonsubmit (dresult );
  24. }
  25. Public static void update <t> (T tentity, Action <t> action)
  26. {
  27. Action (tentity );
  28. Submitchanges ();
  29. }
  30. Public static void insertall <t> (ienumerable <t> tentities) where T: Class
  31. {
  32. VaR table = tablefactory. createtable <t> ();
  33. Table. insertallonsubmit (tentities );
  34. }
  35. Public static void deleteall <t> (ienumerable <t> tentities) where T: Class
  36. {
  37. VaR table = tablefactory. createtable <t> ();
  38. Table. deleteallonsubmit (tentities );
  39. }
  40. Public static ienumerable <t> selectall <t> () where T: Class
  41. {
  42. VaR table = tablefactory. createtable <t> ();
  43. Return table. Select (C => C). asenumerable ();
  44. }
  45. Public static ienumerable <t> where <t> (func <t, bool> predicate) where T: Class
  46. {
  47. VaR table = tablefactory. createtable <t> ();
  48. Return table. Where (predicate). asenumerable ();
  49. }
  50. Public static void submitchanges ()
  51. {
  52. Database. nwdb. submitchanges ();
  53. }
  54. }
  55. }

 

Similarly, we also write some test methods to verify if they are correct.

 

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using Dal;
  6. Using dlinq;
  7. Using toolkits;
  8. Namespace daltest
  9. {
  10. Class Program
  11. {
  12. Static void main (string [] ARGs)
  13. {
  14. // Selectalltest ();
  15. Inserttest ();
  16. Wheretest ();
  17. Updatetest ();
  18. Wheretest ();
  19. Deletetest1 ();
  20. Wheretest ();
  21. Console. writeline ("All testings are success! ");
  22. Console. Read ();
  23. }
  24. Private Static void inserttest ()
  25. {
  26. Customer _ customer = new customer {
  27. Customerid = "Bruce ",
  28. Contactname = "Lee ",
  29. CompanyName = "codingsky ",
  30. City = "Shenzhen "};
  31. Utility. insert (_ customer );
  32. Utility. submitchanges ();
  33. }
  34. Private Static void deletetest1 ()
  35. {
  36. Utility. Delete <customer> (C => C. customerid = "Bruce ");
  37. Utility. submitchanges ();
  38. }
  39. Private Static void deletetest2 ()
  40. {
  41. VaR _ result = utility. Where <customer> (C => C. customerid = "Bruce ");
  42. If (_ result. Count ()> 0)
  43. {
  44. Utility. Delete (_ result. First ());
  45. Utility. submitchanges ();
  46. }
  47. }
  48. Private Static void updatetest ()
  49. {
  50. VaR _ result = utility. Where <customer> (C => C. customerid = "Bruce ");
  51. If (_ result. Count ()> 0)
  52. {
  53. VaR _ customer = _ result. First ();
  54. If (_ customer! = NULL)
  55. {
  56. Utility. Update (_ customer, c =>
  57. {
  58. C. contactname = "Jack ";
  59. C. companyName = "Microsoft ";
  60. C. City = "Beijing ";
  61. });
  62. Utility. submitchanges ();
  63. }
  64. }
  65. }
  66. Private Static void selectalltest ()
  67. {
  68. VaR _ result = utility. selectall <customer> ();
  69. VaR _ list = _ result. tolist ();
  70. If (_ list. Count = 0)
  71. {
  72. Console. writeline ("no result! ");
  73. Return;
  74. }
  75. _ List. foreach (C => console. writeline (stringextension. tostring (c )));
  76. }
  77. Private Static void wheretest ()
  78. {
  79. VaR _ result = utility. Where <customer> (C => C. customerid = "Bruce ");
  80. VaR _ list = _ result. tolist ();
  81. If (_ list. Count = 0)
  82. {
  83. Console. writeline ("no result! ");
  84. Return;
  85. }
  86. Console. writeline ("query result is :");
  87. _ List. foreach (C => console. writeline (stringextension. tostring (c )));
  88. }
  89. }
  90. }

For other Code such as tablefactory, stringextension, see previous: http://blog.csdn.net/fengart/archive/2008/08/19/2798534.aspx

In the preceding test, insert a guy named Bruce in the MERs table, change his contactname to Jack, and delete him.

(The customers table contains too much data, so I commented out the selectalltest method)

The final output is as follows:

Query result is:
Class Name: customer
Customerid: Bruce (string ),
CompanyName: codingsky (string ),
Contactname: Lee (string ),
Contacttitle: (string ),
Address: (string ),
City: Shenzhen (string ),
Region: (string ),
Postalcode: (string ),
Country: (string ),
Phone: (string ),
Fax: (string ),
Orders: system. Data. LINQ. entityset '1 [dlinq. Order] (entityset '1)

Query result is:
Class Name: customer
Customerid: Bruce (string ),
CompanyName: Microsoft (string ),
Contactname: Jack (string ),
Contacttitle: (string ),
Address: (string ),
City: Beijing (string ),
Region: (string ),
Postalcode: (string ),
Country: (string ),
Phone: (string ),
Fax: (string ),
Orders: system. Data. LINQ. entityset '1 [dlinq. Order] (entityset '1)

No result!
All testings are success!

 

If you have any better suggestions, please feel free to contact us! I will be very happy to hear it.

 

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.