optimization mvc code
i have such code
var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true);
if (prj != null)
{
开发者_开发技巧 prj.isPopular = false;
_dataContext.SaveChanges();
}
prj = Details(id);
prj.isPopular = true;
_dataContext.SaveChanges();
idea-i have only one record with value true in field isPopular, so i get it and make false, then i get object by id and make it isPopular true. i don't like 2 calls on savechanges. any ideas?
var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true || p.id ==id);
prj.Single(p => p.isPpopular == true).IsPopular = false;
prj.Single(p => p.isPpopular == id).IsPopular = true;
_dataContext.SaveChanges();
var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true);
if (prj != null)
{
prj.isPopular = false;
}
var prj2 = Details(id);
prj2.isPopular = true;
_dataContext.SaveChanges();
Of course you should find better variable name for "prj2".
精彩评论