Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

If worsening, the custom function Expression is converted to sql.

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "such as worsening custom function Expression into sql". In daily operation, I believe many people have doubts about the problem such as worsening custom function Expression into sql. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts such as worsening custom function Expression into sql. Next, please follow the editor to study!

one。 First of all, set up an extension class, the control can only allow these functions to appear, if there are other functions, it will directly report an exception.

1 public static class SQLMethods 2 {3 public static bool DB_In (this T t, List list) / / in 4 {5 return true; 6} 7 public static Boolean DB_NotIn (this T t, List list) / / not in 8 {9 return true;10} 11 public static int DB_Length (this string t) / / len () 12 {13 return 0 * * 14} 15 public static bool DB_Like (this string t, string str) / / like16 {17 return true;18} 19 public static bool DB_NotLike (this string t, string str) / / not like 20 {21 return true;22} 23}

View Code

If we want to generate sql, then the return value of the function is not interesting. Len (), like, and not can only be for string types, and in and not in can be for all types.

two。 Deal with the logic related to different functions.

1. Determine whether the namespace of the class in which the function is located is what we require.

two。 According to the name of the function. An unexpected function throws an exception

The code is as follows. "

1 private string DealMethodsCall (MethodCallExpression m_exp) 2 {3 var k = masked exp.4 var g = k.Arguments [0]; 5 / the class name of the control function. 6 if (k.Method.DeclaringType! = typeof (SQLMethods)) 7 {8 throw new Exception ("unrecognized function") 9} 10 switch (k.Method.Name) 11 {12 case "DB_Length": 13 {14 var exp = k.Arguments [0]; 15 return "LEN (" + Deal_Expression (exp) + ")" 16} 17 case "DB_In": 18 case "DB_NotIn": 19 {20 var exp1 = k.Arguments [0]; 21 var exp2 = k.Arguments [1]; 22 string methods = string.Empty 23 if (k.Method.Name = = "In") 24 {25 methods = "IN"; 26} 27 else28 {29 methods = "NOT IN" 30} 31 return Deal_Expression (exp1) + methods + Deal_Expression (exp2); 32} 33 case "DB_Like": 34 case "DB_NotLike": 35 {36 var exp1 = k.Arguments [0] 37 var exp2 = k.Arguments [1]; 38 string methods = string.Empty;39 if (k.Method.Name = = "DB_Like") 40 {41 methods = "LIKE" 42} 43 else44 {45 methods = "NOT LIKE"; 46} 47 return Deal_Expression (exp1) + methods + Deal_Expression (exp2) 48 49} 50} 51 / / unknown function 52 throw new Exception ("unexpected function"); 53}

Dealing with correlation function

three。 Debugging result

At this point, the study of "such as worsening the custom function Expression into sql" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report