LambdaHelper.cs 35 KB

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