I have a block of code that pulls the current menu position and compares it to what users selected, if the selection is different the database is updated with the new selection. As I am still new with EF I was hoping for a general code review for suggestions on a possible better, more efficient, and/or easier way to code this. Here is my existing code:
using (var db = new SectionInfoAdmin())
{
var dtCategories = (from n in db.Navigation
where n.SectionID == SectionID
select new
{
n.Category
}).ToList();
string PrimaryCategory = dtCategories[0].Category.ToString();
if (ddlPrimaryCategory.SelectedValue != PrimaryCategory)
{
var Category = (from n in db.Navigation
where n.Category == PrimaryCategory && n.SectionID == SectionID
select n).First();
Category.Category = PrimaryCategory;
}
if (dtCategories.Count > 1)
{
string SecondaryCategory = dtCategories[1].Category.ToString();
if (ddlSecondaryCategory.SelectedValue == SecondaryCategory)
{
var Category = (from n in db.Navigation
where n.Category == SecondaryCategory && n.SectionID == SectionID
select n).First();
Category.Category = SecondaryCategory;
}
}
db.SaveChanges();
lblResults.Text = "Information updated.";
}