Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IPaged<T> : IEnumerable<T>
- {
- /// Get the total entity count.
- int Count { get; }
- /// Get a range of persited entities.
- IEnumerable<T> GetRange(int index, int count, Expression<Func<T, int>> orderLambda);
- }
- public class Paged<T> : IPaged<T>
- {
- private readonly IQueryable<T> source;
- public Paged(IQueryable<T> source)
- {
- this.source = source;
- }
- public IEnumerator<T> GetEnumerator()
- {
- return source.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- public int Count
- {
- get { return source.Count(); }
- }
- public IEnumerable<T> GetRange(int index, int count, Expression<Func<T, int>> orderLambda)
- {
- return source.OrderBy(orderLambda).Skip(index).Take(count);
- }
- public T GetFirst(Expression<Func<T, bool>> whereLambda)
- {
- return source.Where(whereLambda).FirstOrDefault();
- }
- public IEnumerable<T> GetAll()
- {
- return source.ToList();
- }
- public IEnumerable<T> OrderBy(Expression<Func<T, int>> orderLambda)
- {
- return source.OrderBy(orderLambda);
- }
- }
- public class Student1Controller : Controller
- {
- private NorthwindContext NorthwindDB = new NorthwindContext();
- public Student1Controller() {
- }
- public ActionResult Index5() {
- Debug.WriteLine("1ab");
- //List<APerson> Persons = new List<APerson> {
- // new APerson { APersonName="Person A" },
- // new APerson { APersonName="Person B" },
- // new APerson { APersonName="Person C" },
- // new APerson { APersonName="Person D" },
- // };
- //NorthwindDB.APersons.AddRange(Persons);
- //NorthwindDB.SaveChanges();// add person
- Paged<APerson> ob = new Paged<APerson>(NorthwindDB.APersons.AsQueryable());//pass list collection
- APerson ap = ob.GetFirst(c => c.APersonID>1); // pass lamda and get result
- Debug.WriteLine(ap == null?"aaa" : ap.APersonName);
- List<APerson> cList = ob.OrderBy(e => e.APersonID).ToList(); // pass lamda and get result
- IEnumerable<APerson> cList2 = ob.GetRange(1, 2, d => d.APersonID) ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement