LambdaHelper.cs 32 KB

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