Saturday 3 March 2012

C# - LINQ to Entities - How to get first and last record from the table

// entities linq - how to get first record from the table
            using (SchoolEntities schoolContext = new SchoolEntities())
            {
                var firstRecord = (from c in schoolContext.Course
                                   orderby c.CourseID ascending
                                   select c).First();
            }

// now get last record from the table
            using (SchoolEntities schoolContext = new SchoolEntities())
            {
                var lastRecord = (from c in schoolContext.Course
                                  orderby c.CourseID descending
                                  select c).First();
            }



 

No comments:

Post a Comment