首页 > 解决方案 > ASP.NET Core Linq

问题描述

有以下计划的要求:

[HttpPost]
    public async Task<IActionResult> Ref(RefModel model)
    {
        var countreward = from c in db.Person
                            join g in db.Referal
                            on c.AccountID equals g.memb___id into gg
                            from ggg in gg.DefaultIfEmpty()

                            where ggg.referal_reward >= 1
                            where c.RCount >= 1


                            orderby c.AccountID == User.Identity.Name

                            select new Person()
                            {
                                AccountID = c.AccountID,
                                Name = c.Name,
                            };
        var reward = countreward.ToList();

        if (ModelState.IsValid)
        {
            if (reward.Count >= 1)
            {
                CSData bonus = db.CSData.First(p => p.AccountID == User.Identity.Name);
                bonus.GP+= 10 * reward.Count;

                db.CSData.Update(bonus);
                await db.SaveChangesAsync();

                ViewBag.Message = "Yes";
            }
            else
            {
                ViewBag.Message = "No";
            }
        }
        return View(model);
    }

单击按钮后,我从推荐表中获得了我需要的所有推荐,将他们的登录名与 Person 表的所需条件进行比较,然后我的登录名得到 + = 10。我如何向那些获得 10 分的人发送 5 分?谢谢!

标签: c#linqasp.net-core

解决方案


在添加之前await db.SaveChangesAsync();

foreach(var person in reward)
{
    CSData bonus = db.CSData.First(p => p.AccountID == person.AccountID);
    bonus.GP += 5;

    db.CSData.Update(bonus);
}

你真的应该从阅读一本关于基础知识的书开始学习。当您陷入诸如迭代列表之类的简单事情上时,您不会给自己任何好处。在执行访问数据库和通过网络进行通信等复杂的事情之前,请确保您可以完成基本操作。在跑步之前先学会走路。


推荐阅读