GridResultSet.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Reflection;
  7. using System.Linq.Expressions;
  8. namespace Bowin.Common.Linq.Entity
  9. {
  10. [Serializable]
  11. public class GridResultSet<TEntity> : IGridResultSet<TEntity>
  12. {
  13. private OrderByStatementView _orderby;
  14. private int listComparision(TEntity source, TEntity target)
  15. {
  16. var entityType = typeof(TEntity);
  17. var propertyInfo = entityType.GetProperty(_orderby.OrderBy);
  18. if (propertyInfo == null)
  19. {
  20. throw new Exception("类" + entityType.Name + "不包含" + _orderby.OrderBy + "属性。");
  21. }
  22. var sourceValue = propertyInfo.GetValue(source, null);
  23. var targetValue = propertyInfo.GetValue(target, null);
  24. if (_orderby.isAsc)
  25. {
  26. return Convert.ToString(sourceValue).CompareTo(Convert.ToString(targetValue));
  27. }
  28. else
  29. {
  30. return Convert.ToString(targetValue).CompareTo(Convert.ToString(sourceValue));
  31. }
  32. }
  33. private List<TEntity> _rows;
  34. public List<TEntity> rows {
  35. get
  36. {
  37. return _rows;
  38. }
  39. set
  40. {
  41. _orderby = (OrderByStatementView)HttpContext.Current.Session["Bowin_Common_Linq_Entity_CurOrderby"];
  42. if (_orderby != null)
  43. {
  44. value.Sort(new Comparison<TEntity>(listComparision));
  45. HttpContext.Current.Session["Bowin_Common_Linq_Entity_CurOrderby"] = null;
  46. }
  47. _rows = value;
  48. }
  49. }
  50. public int total { get; set; }
  51. }
  52. }