LambdaHelper.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. namespace IwbZero.ToolCommon.Lambda
  8. {
  9. public static class LambdaHelper
  10. {
  11. /// <summary>
  12. /// 创建主键为Id的查询表达式
  13. /// </summary>
  14. /// <typeparam name="TEntity"></typeparam>
  15. /// <typeparam name="TPrimaryKey"></typeparam>
  16. /// <param name="id"></param>
  17. /// <returns></returns>
  18. public static Expression<Func<TEntity, bool>> CreatePrimaryKeyExp<TEntity, TPrimaryKey>(TPrimaryKey id)
  19. {
  20. ParameterExpression left = Expression.Parameter(typeof(TEntity), "a");
  21. MemberExpression member = Expression.PropertyOrField(left, "Id");
  22. ConstantExpression constant = Expression.Constant(id, typeof(TPrimaryKey));
  23. Expression right = Expression.Equal(member, constant);
  24. Expression<Func<TEntity, bool>> exp = Expression.Lambda<Func<TEntity, bool>>(right, left);
  25. return exp;
  26. }
  27. /// <summary>
  28. /// 创建某字段的查询表达式
  29. /// </summary>
  30. /// <typeparam name="TEntity"></typeparam>
  31. /// <param name="name"></param>
  32. /// <param name="value"></param>
  33. /// <returns></returns>
  34. public static Expression<Func<TEntity, bool>> CreateFieldExp<TEntity>(string name, object value)
  35. {
  36. ParameterExpression left = Expression.Parameter(typeof(TEntity), "a");
  37. MemberExpression member = Expression.PropertyOrField(left, name);
  38. ConstantExpression constant = Expression.Constant(value, typeof(TEntity).GetProperty(name)?.PropertyType ?? throw new InvalidOperationException());
  39. Expression right = Expression.Equal(member, constant);
  40. Expression<Func<TEntity, bool>> exp = Expression.Lambda<Func<TEntity, bool>>(right, left);
  41. return exp;
  42. }
  43. #region ORDER BY
  44. /// <summary>
  45. /// 创建lambda表达式:p=>p.propertyName
  46. /// </summary>
  47. /// <typeparam name="T"></typeparam>
  48. /// <typeparam name="TKey"></typeparam>
  49. /// <param name="propertyName"></param>
  50. /// <returns></returns>
  51. public static Expression<Func<T, TKey>> GetOrderExp<T, TKey>(string propertyName)
  52. {
  53. ParameterExpression parameter = Expression.Parameter(typeof(T), "o");
  54. return Expression.Lambda<Func<T, TKey>>(Expression.Property(parameter, propertyName), parameter);
  55. }
  56. #endregion ORDER BY
  57. #region WHERE
  58. /// <summary>
  59. /// 创建 LambdaExpression<Func<T, bool>>
  60. /// </summary>
  61. /// <typeparam name="T"></typeparam>
  62. /// <param name="obj"></param>
  63. /// <param name="left"></param>
  64. /// <returns></returns>
  65. public static Expression<Func<T, bool>> GetExp<T>(this LambdaObject obj, ParameterExpression left = null)
  66. {
  67. Expression<Func<T, bool>> finalExp = null;
  68. left = left ?? Expression.Parameter(typeof(T), "a");
  69. Expression right = GetRightExp<T>(obj, left);
  70. if (right != null) finalExp = Expression.Lambda<Func<T, bool>>(right, left);
  71. return finalExp;
  72. }
  73. /// <summary>
  74. /// 创建 LambdaExpression<Func<T, bool>>
  75. /// </summary>
  76. /// <typeparam name="T"></typeparam>
  77. /// <param name="objs"></param>
  78. /// <param name="left"></param>
  79. /// <returns></returns>
  80. public static Expression<Func<T, bool>> GetExp<T>(this List<LambdaObject> objs, ParameterExpression left = null)
  81. {
  82. Expression<Func<T, bool>> finalExp = null;
  83. left = left ?? Expression.Parameter(typeof(T), "a");
  84. Expression right = null;
  85. foreach (var obj in objs)
  86. {
  87. Expression exp = GetRightExp<T>(obj, left);
  88. right = right == null ? exp : CombineExp(right, exp, obj.LogicType);
  89. }
  90. if (right != null) finalExp = Expression.Lambda<Func<T, bool>>(right, left);
  91. return finalExp;
  92. }
  93. /// <summary>
  94. /// 创建 MemberExpression
  95. /// </summary>
  96. /// <typeparam name="T"></typeparam>
  97. /// <param name="obj"></param>
  98. /// <param name="left"></param>
  99. /// <returns></returns>
  100. public static Expression GetRightExp<T>(LambdaObject obj, ParameterExpression left)
  101. {
  102. left = left ?? Expression.Parameter(typeof(T), "a");//创建参数a
  103. string fieldName = obj.FieldName;
  104. //var fieldValue = obj.FieldValue;
  105. MemberExpression member = Expression.PropertyOrField(left, fieldName);
  106. Expression right = null;
  107. if (obj.Children != null && obj.Children.Any())
  108. right = GetRightExps<T>(obj.Children, left);
  109. else
  110. {
  111. Type t = typeof(T).GetProperty(fieldName)?.PropertyType;
  112. if (t == null)
  113. {
  114. fieldName = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
  115. t = typeof(T).GetProperty(fieldName)?.PropertyType;
  116. t = t ?? GetFiledType(obj.FieldType);
  117. }
  118. var newType = CheckNullableAndConvertType(t);
  119. var value = Convert.ChangeType(obj.FieldValue, newType);
  120. ConstantExpression constant = Expression.Constant(value, t);
  121. //var type = typeof(T).GetProperty(fieldName) ?? throw new InvalidOperationException();
  122. //ConstantExpression constant = Expression.Constant(obj.FieldValue);
  123. switch ((int)obj.ExpType)
  124. {
  125. case 0:
  126. right = constant != null ? Expression.Equal(member, constant) : null;
  127. break;
  128. case 1:
  129. right = constant != null ? Expression.NotEqual(member, constant) : null;
  130. break;
  131. case 2:
  132. right = constant != null ? Expression.GreaterThan(member, constant) : null;
  133. break;
  134. case 3:
  135. right = constant != null ? Expression.LessThan(member, constant) : null;
  136. break;
  137. case 4:
  138. right = constant != null ? Expression.GreaterThanOrEqual(member, constant) : null;
  139. break;
  140. case 5:
  141. right = constant != null ? Expression.LessThanOrEqual(member, constant) : null;
  142. break;
  143. case 6:
  144. right = GetRightExp_Contains<T>(obj, left);
  145. break;
  146. case 7:
  147. right = GetRightExp_NotContains<T>(obj, left);
  148. break;
  149. case 8:
  150. break;
  151. }
  152. }
  153. return right;
  154. }
  155. /// <summary>
  156. /// 创建 MemberExpression
  157. /// </summary>
  158. /// <typeparam name="T"></typeparam>
  159. /// <param name="objs"></param>
  160. /// <param name="left"></param>
  161. /// <returns></returns>
  162. public static Expression GetRightExps<T>(List<LambdaObject> objs, ParameterExpression left)
  163. {
  164. left = left ?? Expression.Parameter(typeof(T), "a");//创建参数a
  165. Expression right = null;
  166. foreach (var obj in objs)
  167. {
  168. Expression exp = GetRightExp<T>(obj, left);
  169. right = right == null ? exp : CombineExp(right, exp, obj.LogicType);
  170. }
  171. return right;
  172. }
  173. /// <summary>
  174. /// 创建 ConstantExpression
  175. /// </summary>
  176. private static ConstantExpression GetConstantExp<T>(LambdaObject obj)
  177. {
  178. var fieldValue = obj.FieldValue;
  179. //Type t = GetFiledType(obj.FieldType);
  180. ConstantExpression constant = null; // Expression.Constant(fieldValue, t);;
  181. switch ((int)obj.FieldType)
  182. {
  183. case 0:
  184. constant = Expression.Constant(fieldValue is string ? fieldValue : fieldValue.ToString(), typeof(string));
  185. break;
  186. case 1:
  187. if (fieldValue is int)
  188. constant = Expression.Constant(fieldValue, typeof(int));
  189. else if (int.TryParse(fieldValue.ToString(), out int newValue))
  190. constant = Expression.Constant(newValue, typeof(int));
  191. break;
  192. case 2:
  193. if (fieldValue is int)
  194. constant = Expression.Constant(fieldValue, typeof(int?));
  195. else if (int.TryParse(fieldValue.ToString(), out int newValue))
  196. constant = Expression.Constant(newValue, typeof(int?));
  197. break;
  198. case 3:
  199. if (fieldValue is bool)
  200. constant = Expression.Constant(fieldValue, typeof(bool));
  201. else if (bool.TryParse(fieldValue.ToString(), out bool newValue))
  202. constant = Expression.Constant(newValue, typeof(bool));
  203. break;
  204. case 4:
  205. if (fieldValue is bool)
  206. constant = Expression.Constant(fieldValue, typeof(bool?));
  207. else if (bool.TryParse(fieldValue.ToString(), out bool newValue))
  208. constant = Expression.Constant(newValue, typeof(bool?));
  209. break;
  210. case 5:
  211. if (fieldValue is DateTime)
  212. constant = Expression.Constant(fieldValue, typeof(DateTime));
  213. else if (DateTime.TryParse(fieldValue.ToString(), out DateTime newValue))
  214. constant = Expression.Constant(newValue, typeof(DateTime));
  215. break;
  216. case 6:
  217. if (fieldValue is DateTime)
  218. constant = Expression.Constant(fieldValue, typeof(DateTime?));
  219. else if (DateTime.TryParse(fieldValue.ToString(), out DateTime newValue))
  220. constant = Expression.Constant(newValue, typeof(DateTime?));
  221. break;
  222. case 7:
  223. if (fieldValue is long)
  224. constant = Expression.Constant(fieldValue, typeof(long));
  225. else if (long.TryParse(fieldValue.ToString(), out long newValue))
  226. constant = Expression.Constant(newValue, typeof(long));
  227. break;
  228. case 8:
  229. if (fieldValue is long)
  230. constant = Expression.Constant(fieldValue, typeof(long?));
  231. else if (long.TryParse(fieldValue.ToString(), out long newValue))
  232. constant = Expression.Constant(newValue, typeof(long?));
  233. break;
  234. case 9:
  235. if (fieldValue is short)
  236. constant = Expression.Constant(fieldValue, typeof(short));
  237. else if (short.TryParse(fieldValue.ToString(), out short newValue))
  238. constant = Expression.Constant(newValue, typeof(short));
  239. break;
  240. case 10:
  241. if (fieldValue is short)
  242. constant = Expression.Constant(fieldValue, typeof(short?));
  243. else if (short.TryParse(fieldValue.ToString(), out short newValue))
  244. constant = Expression.Constant(newValue, typeof(short?));
  245. break;
  246. case 11:
  247. if (fieldValue is float)
  248. constant = Expression.Constant(fieldValue, typeof(float));
  249. else if (float.TryParse(fieldValue.ToString(), out float newValue))
  250. constant = Expression.Constant(newValue, typeof(float));
  251. break;
  252. case 12:
  253. if (fieldValue is float)
  254. constant = Expression.Constant(fieldValue, typeof(float?));
  255. else if (float.TryParse(fieldValue.ToString(), out float newValue))
  256. constant = Expression.Constant(newValue, typeof(float?));
  257. break;
  258. case 13:
  259. if (fieldValue is decimal)
  260. constant = Expression.Constant(fieldValue, typeof(decimal));
  261. else if (decimal.TryParse(fieldValue.ToString(), out decimal newValue))
  262. constant = Expression.Constant(newValue, typeof(decimal));
  263. break;
  264. case 14:
  265. if (fieldValue is decimal)
  266. constant = Expression.Constant(fieldValue, typeof(decimal?));
  267. else if (decimal.TryParse(fieldValue.ToString(), out decimal newValue))
  268. constant = Expression.Constant(newValue, typeof(decimal?));
  269. break;
  270. case 15:
  271. if (fieldValue is double)
  272. constant = Expression.Constant(fieldValue, typeof(double));
  273. else if (double.TryParse(fieldValue.ToString(), out double newValue))
  274. constant = Expression.Constant(newValue, typeof(double));
  275. break;
  276. case 16:
  277. if (fieldValue is double)
  278. constant = Expression.Constant(fieldValue, typeof(double?));
  279. else if (double.TryParse(fieldValue.ToString(), out double newValue))
  280. constant = Expression.Constant(newValue, typeof(double?));
  281. break;
  282. }
  283. return constant;
  284. }
  285. private static Type CheckNullableAndConvertType(Type theType)
  286. {
  287. if (theType.IsGenericType && theType.GetGenericTypeDefinition() == typeof(Nullable<>))
  288. {
  289. return theType.GetGenericArguments()[0];
  290. }
  291. return theType;
  292. }
  293. private static Type GetFiledType(LambdaFieldType type)
  294. {
  295. switch (type)
  296. {
  297. case LambdaFieldType.S: return typeof(string);
  298. case LambdaFieldType.I: return typeof(int);
  299. case LambdaFieldType.Inull: return typeof(int?);
  300. case LambdaFieldType.B: return typeof(bool);
  301. case LambdaFieldType.Bnull: return typeof(bool?);
  302. case LambdaFieldType.D: return typeof(DateTime);
  303. case LambdaFieldType.Dnull: return typeof(DateTime?);
  304. case LambdaFieldType.L: return typeof(long);
  305. case LambdaFieldType.Lnull: return typeof(long?);
  306. case LambdaFieldType.Short: return typeof(short);
  307. case LambdaFieldType.Snull: return typeof(short?);
  308. case LambdaFieldType.F: return typeof(float);
  309. case LambdaFieldType.Fnull: return typeof(float?);
  310. case LambdaFieldType.Decimal: return typeof(decimal);
  311. case LambdaFieldType.DecimalNull: return typeof(decimal?);
  312. case LambdaFieldType.Double: return typeof(double);
  313. case LambdaFieldType.DoubleNull: return typeof(double?);
  314. default: return null;
  315. }
  316. }
  317. /// <summary>
  318. /// 创建lambda表达式:p=>p.propertyName.Contains(propertyValue)
  319. /// </summary>
  320. /// <typeparam name="T"></typeparam>
  321. /// <param name="obj"></param>
  322. /// <param name="left"></param>
  323. /// <returns></returns>
  324. public static Expression GetRightExp_Contains<T>(LambdaObject obj, ParameterExpression left = null)
  325. {
  326. string fieldName = obj.FieldName;
  327. var fieldValue = obj.FieldValue;
  328. left = left ?? Expression.Parameter(typeof(T), "a");//创建参数a
  329. MemberExpression member = Expression.PropertyOrField(left, fieldName);
  330. Expression right = null;
  331. switch (obj.FieldType)
  332. {
  333. case LambdaFieldType.S:
  334. var method1 = typeof(string).GetMethod("Contains", new[] { typeof(string) });
  335. if (method1 == null) return null;
  336. if (fieldValue is string)
  337. {
  338. ConstantExpression constant = Expression.Constant(fieldValue.ToString());//创建常数
  339. right = Expression.Call(member, method1, constant);
  340. }
  341. else
  342. {
  343. foreach (var o in (IEnumerable)fieldValue)
  344. {
  345. ConstantExpression constant = Expression.Constant(o);
  346. var temp = Expression.Call(member, method1, constant);
  347. right = right == null ? temp : CombineExp(right, temp, LogicType.Or);
  348. }
  349. }
  350. break;
  351. case LambdaFieldType.I:
  352. var method2 = typeof(int).GetMethod("Contains", new[] { typeof(int) });
  353. if (method2 == null) return null;
  354. if (fieldValue is int)
  355. {
  356. ConstantExpression constant = Expression.Constant(fieldValue.ToString());
  357. right = Expression.Call(member, method2, constant);
  358. }
  359. else
  360. {
  361. foreach (var o in (IEnumerable)fieldValue)
  362. {
  363. ConstantExpression constant = Expression.Constant(o);
  364. var temp = Expression.Call(member, method2, constant);
  365. right = right == null ? temp : CombineExp(right, temp, LogicType.Or);
  366. }
  367. }
  368. break;
  369. case LambdaFieldType.B:
  370. var method3 = typeof(bool).GetMethod("Contains", new[] { typeof(bool) });
  371. if (method3 == null) return null;
  372. if (fieldValue is bool)
  373. {
  374. ConstantExpression constant = Expression.Constant(fieldValue.ToString());
  375. right = Expression.Call(member, method3, constant);
  376. }
  377. break;
  378. }
  379. return right;
  380. }
  381. /// <summary>
  382. /// 创建lambda表达式:!(p=>p.propertyName.Contains(propertyValue))
  383. /// </summary>
  384. /// <typeparam name="T"></typeparam>
  385. /// <param name="obj"></param>
  386. /// <param name="left"></param>
  387. /// <returns></returns>
  388. public static Expression GetRightExp_NotContains<T>(LambdaObject obj, ParameterExpression left = null)
  389. {
  390. return Expression.Not(GetRightExp_Contains<T>(obj, left));
  391. }
  392. /// <summary>
  393. /// 拼接 Expression
  394. /// </summary>
  395. /// <param name="exp1"></param>
  396. /// <param name="exp2"></param>
  397. /// <param name="type"></param>
  398. /// <returns></returns>
  399. public static Expression CombineExp(this Expression exp1, Expression exp2, LogicType type)
  400. {
  401. Expression exp = exp1;
  402. if (exp1 == null || exp2 == null)
  403. {
  404. return exp1;
  405. }
  406. switch ((int)type)
  407. {
  408. case 0:
  409. exp = Expression.And(exp1, exp2);
  410. break;
  411. case 1:
  412. exp = Expression.Or(exp1, exp2);
  413. break;
  414. }
  415. return exp;
  416. }
  417. #endregion WHERE
  418. #region 暂未用到
  419. /// <summary>
  420. /// 创建 MemberExpression
  421. /// </summary>
  422. /// <typeparam name="T"></typeparam>
  423. /// <param name="objs"></param>
  424. /// <param name="left"></param>
  425. /// <returns></returns>
  426. public static Expression GetRightExp2<T>(List<LambdaObject> objs, ParameterExpression left)
  427. {
  428. Expression right = null;
  429. foreach (var obj in objs)
  430. {
  431. Expression exp = null;
  432. if (obj.Children.Any())
  433. {
  434. exp = GetRightExps<T>(obj.Children, left);
  435. right = right == null ? exp : CombineExp(right, exp, obj.LogicType);
  436. continue;
  437. }
  438. switch ((int)obj.ExpType)
  439. {
  440. case 0:
  441. exp = GetRightExp_Equal<T>(obj, left);
  442. break;
  443. case 1:
  444. exp = GetRightExp_NotEqual<T>(obj, left);
  445. break;
  446. case 2:
  447. break;
  448. case 3:
  449. break;
  450. case 4:
  451. break;
  452. case 5:
  453. break;
  454. case 6:
  455. break;
  456. case 7:
  457. exp = GetRightExp_Contains<T>(obj);
  458. break;
  459. case 8:
  460. exp = GetRightExp_NotContains<T>(obj);
  461. break;
  462. }
  463. right = right == null ? exp : CombineExp(right, exp, obj.LogicType);
  464. }
  465. return right;
  466. }
  467. /// <summary>
  468. /// 创建lambda表达式:p=>p.propertyName == propertyValue
  469. /// </summary>
  470. public static Expression GetRightExp_Equal<T>(LambdaObject obj, ParameterExpression left = null)
  471. {
  472. //Expression<Func<T, bool>> exp = null;
  473. string fieldName = obj.FieldName;
  474. var fieldValue = obj.FieldValue;
  475. left = left ?? Expression.Parameter(typeof(T), "a");//创建参数a
  476. MemberExpression member = Expression.PropertyOrField(left, fieldName);
  477. Expression right = null;
  478. switch ((int)obj.FieldType)
  479. {
  480. case 0:
  481. if (fieldValue is string)
  482. {
  483. ConstantExpression constant = Expression.Constant(fieldValue);//创建常数
  484. //exp= Expression.Lambda<Func<T, bool>>(Expression.Equal(member, constant), left);
  485. right = Expression.Equal(member, constant);
  486. }
  487. //else if (os is List<string>)
  488. //{
  489. // foreach (var o in (List<string>) os)
  490. // {
  491. // ConstantExpression constant = Expression.Constant(o);
  492. // var temp = Expression.Equal(member, constant);
  493. // right = CombineExp(right,temp,obj.LogicType);
  494. // }
  495. //}
  496. break;
  497. case 1:
  498. if (fieldValue is int)
  499. {
  500. ConstantExpression constant = Expression.Constant(fieldValue);
  501. right = Expression.Equal(member, constant);
  502. }
  503. break;
  504. case 2:
  505. if (fieldValue is bool)
  506. {
  507. ConstantExpression constant = Expression.Constant(fieldValue);
  508. right = Expression.Equal(member, constant);
  509. }
  510. break;
  511. }
  512. return right;
  513. }
  514. /// <summary>
  515. /// 创建lambda表达式:p=>p.propertyName != propertyValue
  516. /// </summary>
  517. public static Expression GetRightExp_NotEqual<T>(LambdaObject obj, ParameterExpression left = null)
  518. {
  519. string fieldName = obj.FieldName;
  520. var fieldValue = obj.FieldValue;
  521. left = left ?? Expression.Parameter(typeof(T), "a");//创建参数a
  522. MemberExpression member = Expression.PropertyOrField(left, fieldName);
  523. Expression right = null;
  524. switch ((int)obj.FieldType)
  525. {
  526. case 0:
  527. if (fieldValue is string)
  528. {
  529. ConstantExpression constant = Expression.Constant(fieldValue);
  530. right = Expression.NotEqual(member, constant);
  531. }
  532. break;
  533. case 1:
  534. if (fieldValue is int)
  535. {
  536. ConstantExpression constant = Expression.Constant(fieldValue);
  537. right = Expression.NotEqual(member, constant);
  538. }
  539. break;
  540. case 2:
  541. if (fieldValue is bool)
  542. {
  543. ConstantExpression constant = Expression.Constant(fieldValue);
  544. right = Expression.NotEqual(member, constant);
  545. }
  546. break;
  547. }
  548. return right;
  549. }
  550. /// <summary>
  551. /// 创建lambda表达式:p=>true
  552. /// </summary>
  553. /// <typeparam name="T"></typeparam>
  554. /// <returns></returns>
  555. public static Expression<Func<T, bool>> True<T>()
  556. {
  557. return p => true;
  558. }
  559. /// <summary>
  560. /// 创建lambda表达式:p=>false
  561. /// </summary>
  562. /// <typeparam name="T"></typeparam>
  563. /// <returns></returns>
  564. public static Expression<Func<T, bool>> False<T>()
  565. {
  566. return p => false;
  567. }
  568. /// <summary>
  569. /// 创建lambda表达式:p=>p.propertyName
  570. /// </summary>
  571. /// <typeparam name="T"></typeparam>
  572. /// <typeparam name="TKey"></typeparam>
  573. /// <param name="propertyName"></param>
  574. /// <returns></returns>
  575. public static Expression<Func<T, TKey>> GetOrderExpression<T, TKey>(string propertyName)
  576. {
  577. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
  578. return Expression.Lambda<Func<T, TKey>>(Expression.Property(parameter, propertyName), parameter);
  579. }
  580. /// <summary>
  581. /// 创建lambda表达式:p=>p.propertyName == propertyValue
  582. /// </summary>
  583. /// <returns></returns>
  584. public static Expression<Func<T, bool>> CreateEqual<T>(string propertyName, string propertyValue)
  585. {
  586. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
  587. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  588. ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
  589. return Expression.Lambda<Func<T, bool>>(Expression.Equal(member, constant), parameter);
  590. }
  591. /// <summary>
  592. /// 创建lambda表达式:p=>p.propertyName != propertyValue
  593. /// </summary>
  594. /// <typeparam name="T"></typeparam>
  595. /// <param name="propertyName"></param>
  596. /// <param name="propertyValue"></param>
  597. /// <returns></returns>
  598. public static Expression<Func<T, bool>> CreateNotEqual<T>(string propertyName, string propertyValue)
  599. {
  600. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
  601. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  602. ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
  603. return Expression.Lambda<Func<T, bool>>(Expression.NotEqual(member, constant), parameter);
  604. }
  605. /// <summary>
  606. /// 创建lambda表达式:p=>p.propertyName > propertyValue
  607. /// </summary>
  608. /// <typeparam name="T"></typeparam>
  609. /// <returns></returns>
  610. public static Expression<Func<T, bool>> CreateGreaterThan<T>(string propertyName, string propertyValue)
  611. {
  612. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
  613. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  614. ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
  615. return Expression.Lambda<Func<T, bool>>(Expression.GreaterThan(member, constant), parameter);
  616. }
  617. /// <summary>
  618. /// 创建lambda表达式:p=>p.propertyName < propertyValue />
  619. /// </summary>
  620. /// <typeparam name="T"></typeparam>
  621. /// <returns></returns>
  622. public static Expression<Func<T, bool>> CreateLessThan<T>(string propertyName, string propertyValue)
  623. {
  624. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
  625. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  626. ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
  627. return Expression.Lambda<Func<T, bool>>(Expression.LessThan(member, constant), parameter);
  628. }
  629. /// <summary>
  630. /// 创建lambda表达式:p=>p.propertyName >= propertyValue
  631. /// </summary>
  632. /// <typeparam name="T"></typeparam>
  633. /// <returns></returns>
  634. public static Expression<Func<T, bool>> CreateGreaterThanOrEqual<T>(string propertyName, string propertyValue)
  635. {
  636. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
  637. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  638. ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
  639. return Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
  640. }
  641. // ReSharper disable once InvalidXmlDocComment
  642. /// <summary>
  643. /// 创建lambda表达式:p=>p.propertyName <= propertyValue
  644. /// </summary>
  645. /// <typeparam name="T"></typeparam>
  646. /// <returns></returns>
  647. public static Expression<Func<T, bool>> CreateLessThanOrEqual<T>(string propertyName, string propertyValue)
  648. {
  649. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
  650. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  651. ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
  652. return Expression.Lambda<Func<T, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
  653. }
  654. /// <summary>
  655. /// 创建lambda表达式:p=>p.propertyName.Contains(propertyValue)
  656. /// </summary>
  657. /// <typeparam name="T"></typeparam>
  658. /// <returns></returns>
  659. public static Expression<Func<T, bool>> GetContains<T>(string propertyName, string propertyValue)
  660. {
  661. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
  662. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  663. MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
  664. ConstantExpression constant = Expression.Constant(propertyValue, typeof(string));
  665. return Expression.Lambda<Func<T, bool>>(Expression.Call(member, method ?? throw new InvalidOperationException(), constant), parameter);
  666. }
  667. /// <summary>
  668. /// 创建lambda表达式:!(p=>p.propertyName.Contains(propertyValue))
  669. /// </summary>
  670. /// <typeparam name="T"></typeparam>
  671. /// <returns></returns>
  672. public static Expression<Func<T, bool>> GetNotContains<T>(string propertyName, string propertyValue)
  673. {
  674. ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
  675. MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
  676. MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
  677. ConstantExpression constant = Expression.Constant(propertyValue, typeof(string));
  678. return Expression.Lambda<Func<T, bool>>(Expression.Not(Expression.Call(member, method ?? throw new InvalidOperationException(), constant)), parameter);
  679. }
  680. #endregion 暂未用到
  681. }
  682. public class LambdaObject
  683. {
  684. public LambdaObject()
  685. {
  686. }
  687. public LambdaObject(string fieldName, object fieldValue, LambdaExpType expType = LambdaExpType.Contains, LambdaFieldType fieldType = LambdaFieldType.S, LogicType logicType = LogicType.And)
  688. {
  689. FieldName = fieldName;
  690. FieldValue = fieldValue;
  691. FieldType = fieldType;
  692. ExpType = expType;
  693. LogicType = logicType;
  694. }
  695. public string FieldName { get; set; }
  696. public object FieldValue { get; set; }
  697. public LambdaFieldType FieldType { get; set; }
  698. public LambdaExpType ExpType { get; set; }
  699. public LogicType LogicType { get; set; }
  700. public List<LambdaObject> Children { get; set; }
  701. }
  702. /// <summary>
  703. /// 表达式类型
  704. /// </summary>
  705. public enum LambdaExpType
  706. {
  707. Equal
  708. , NotEqual
  709. , Greater
  710. , Less
  711. , GreaterOrEqual
  712. , LessOrEqual
  713. , Contains
  714. , NotContains
  715. }
  716. /// <summary>
  717. /// 字段类型
  718. /// </summary>
  719. public enum LambdaFieldType
  720. {
  721. /// <summary>
  722. /// string
  723. /// </summary>
  724. S = 0,
  725. /// <summary>
  726. /// int
  727. /// </summary>
  728. I = 1,
  729. /// <summary>
  730. /// int?
  731. /// </summary>
  732. Inull = 2,
  733. /// <summary>
  734. /// bool
  735. /// </summary>
  736. B = 3,
  737. /// <summary>
  738. /// bool?
  739. /// </summary>
  740. Bnull = 4,
  741. /// <summary>
  742. /// Datetime
  743. /// </summary>
  744. D = 5,
  745. /// <summary>
  746. /// Datetime?
  747. /// </summary>
  748. Dnull = 6,
  749. /// <summary>
  750. /// long
  751. /// </summary>
  752. L = 7,
  753. /// <summary>
  754. /// long?
  755. /// </summary>
  756. Lnull = 8,
  757. /// <summary>
  758. /// short
  759. /// </summary>
  760. Short = 9,
  761. /// <summary>
  762. /// Short?
  763. /// </summary>
  764. Snull = 10,
  765. /// <summary>
  766. /// float
  767. /// </summary>
  768. F = 11,
  769. /// <summary>
  770. /// float
  771. /// </summary>
  772. Fnull = 12,
  773. /// <summary>
  774. /// Decimal
  775. /// </summary>
  776. Decimal = 13,
  777. /// <summary>
  778. /// Decimal
  779. /// </summary>
  780. DecimalNull = 14,
  781. /// <summary>
  782. /// Double
  783. /// </summary>
  784. Double = 15,
  785. /// <summary>
  786. /// Double
  787. /// </summary>
  788. DoubleNull = 16
  789. }
  790. /// <summary>
  791. /// 拼接时 AND || OR
  792. /// </summary>
  793. public enum LogicType
  794. {
  795. And,
  796. Or
  797. }
  798. }