TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
acme deepak
NA
30
602
C# code to me made better with Lambda expressions
Jan 15 2020 7:44 PM
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));
}
}
}
Reply
Answers (
2
)
Convert rupees to word including paisa in asp.net c#
How to make a simple loop until last record