开发者

optimization mvc code

开发者 https://www.devze.com 2022-12-23 00:27 出处:网络
i have such code var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true); if (prj != null)

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".

0

精彩评论

暂无评论...
验证码 换一张
取 消