Asked by:
C# code to me made better with Lambda expressions

Question
-
User1182587605 posted
I have a C# code that will perform CRUD operations on data into a grid view. I need to make it work with the lambda expressions. Please find the below code and help me further. I have no prior knowledge of regular expressions. I have given all the methods that need to be converted to Regular expressions and made better if possible at all angles. Please help me with this.
public IQueryable<Student> studentsGrid_GetData() { SchoolContext db = new SchoolContext(); var query = db.Students.Include(s => s.Enrollments.Select(e => e.Course)); return query; } // The id parameter name should match the DataKeyNames value set on the control public void studentsGrid_UpdateItem(int id) { ContosoUniversityModelBinding.Models.Student item = null; // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); } } public void studentsGrid_UpdateItem(int studentID) { using (SchoolContext db = new SchoolContext()) { Student item = null; item = db.Students.Find(studentID); if (item == null) { ModelState.AddModelError("", String.Format("Item with id {0} was not found", studentID)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } } } public void studentsGrid_DeleteItem(int studentID) { using (SchoolContext db = new SchoolContext()) { var item = new Student { StudentID = studentID }; db.Entry(item).State = EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError("", String.Format("Item with id {0} no longer exists in the database.", studentID)); } } }
converting to lambda expressions is a major necessity
Wednesday, January 15, 2020 2:17 PM
All replies
-
User475983607 posted
Your requirement makes no sense. Lambda expressions are anonymous functions that are or can be passed as arguments. You are already using a Lambda by the way...
Can you explain what these Lambda expressions are supposed to do?
C# Programming Guide.
Edit: maybe you mean async/await?
Wednesday, January 15, 2020 2:39 PM -
User1182587605 posted
I want the lambda expressions to shorten the CRUD operations I have and make my code more adaptable and user-friendly. Please help me if that can be done?
Wednesday, January 15, 2020 4:04 PM -
User475983607 posted
I want the lambda expressions to shorten the CRUD operations I have and make my code more adaptable and user-friendly. Please help me if that can be done?
As far as I understand, you have 2 requirements.
- shorten the CRUD operations
- make my code more adaptable and user-friendly.
Lambda expression will not solve these requirements for many reason. Here are a few related to your code.
- Lambda expressions by definition are a single line of code.
- Lambda statements have multiple lines of code but typically no more than two or three.
- Your CRUD methods have hard dependencies on the current context and Entity Framework.
Perhaps you are looking for a the repository or unit of work patterns.
Wednesday, January 15, 2020 5:43 PM