c# - 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 分?谢谢!
解决方案
在添加之前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);
}
你真的应该从阅读一本关于基础知识的书开始学习。当您陷入诸如迭代列表之类的简单事情上时,您不会给自己任何好处。在执行访问数据库和通过网络进行通信等复杂的事情之前,请确保您可以完成基本操作。在跑步之前先学会走路。
推荐阅读
- reactjs - ReactJS - 在 2 个不同的组件之间淡入淡出
- javascript - 有没有办法在反应中渲染组件内部的路线?
- javascript - 密钥未定义 - 我无法删除
- html - 随滚动隐藏的导航栏和标题
- python - 如何替换单词
- c - 是否可以将动态对象包装在 ELF 动态链接的二进制文件中?
- string - 嵌入字符串中的 Goto 函数
- python - 模拟导入模块时调用的函数
- javascript - JavaScript 在用户输入后清除文本区域
- powershell - From PowerShell Get Refresh History In Group from Power BI REST APIs and export do CSV