Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public interface IPaged<T> : IEnumerable<T>
  2. {
  3. /// Get the total entity count.
  4. int Count { get; }
  5.  
  6. /// Get a range of persited entities.
  7. IEnumerable<T> GetRange(int index, int count, Expression<Func<T, int>> orderLambda);
  8. }
  9.  
  10.  
  11. public class Paged<T> : IPaged<T>
  12. {
  13. private readonly IQueryable<T> source;
  14.  
  15. public Paged(IQueryable<T> source)
  16. {
  17. this.source = source;
  18. }
  19.  
  20. public IEnumerator<T> GetEnumerator()
  21. {
  22. return source.GetEnumerator();
  23. }
  24.  
  25. IEnumerator IEnumerable.GetEnumerator()
  26. {
  27. return GetEnumerator();
  28. }
  29.  
  30. public int Count
  31. {
  32. get { return source.Count(); }
  33. }
  34.  
  35. public IEnumerable<T> GetRange(int index, int count, Expression<Func<T, int>> orderLambda)
  36. {
  37. return source.OrderBy(orderLambda).Skip(index).Take(count);
  38. }
  39.  
  40. public T GetFirst(Expression<Func<T, bool>> whereLambda)
  41. {
  42. return source.Where(whereLambda).FirstOrDefault();
  43. }
  44.  
  45. public IEnumerable<T> GetAll()
  46. {
  47. return source.ToList();
  48. }
  49. public IEnumerable<T> OrderBy(Expression<Func<T, int>> orderLambda)
  50. {
  51. return source.OrderBy(orderLambda);
  52. }
  53. }
  54.  
  55. public class Student1Controller : Controller
  56. {
  57. private NorthwindContext NorthwindDB = new NorthwindContext();
  58. public Student1Controller() {
  59.  
  60. }
  61. public ActionResult Index5() {
  62. Debug.WriteLine("1ab");
  63. //List<APerson> Persons = new List<APerson> {
  64. // new APerson { APersonName="Person A" },
  65. // new APerson { APersonName="Person B" },
  66. // new APerson { APersonName="Person C" },
  67. // new APerson { APersonName="Person D" },
  68. // };
  69.  
  70. //NorthwindDB.APersons.AddRange(Persons);
  71. //NorthwindDB.SaveChanges();// add person
  72.  
  73. Paged<APerson> ob = new Paged<APerson>(NorthwindDB.APersons.AsQueryable());//pass list collection
  74. APerson ap = ob.GetFirst(c => c.APersonID>1); // pass lamda and get result
  75.  
  76. Debug.WriteLine(ap == null?"aaa" : ap.APersonName);
  77. List<APerson> cList = ob.OrderBy(e => e.APersonID).ToList(); // pass lamda and get result
  78. IEnumerable<APerson> cList2 = ob.GetRange(1, 2, d => d.APersonID) ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement