简体   繁体   English

如何在我的ASP.NET MVC 5项目中使用LINQ LAMBDA更新记录

[英]How to update a record using LINQ LAMBDA in my ASP.NET MVC 5 Project

I am new to entity framework and LINQ. 我是实体框架和LINQ的新手。 I am stuck at an issue where I need to firstly check if the record already exists, if it exists then I need to update the record with column RESUMEID accordingly. 我陷入一个问题,我需要首先检查记录是否已经存在,如果存在,那么我需要相应地使用RESUMEID列更新记录。 If not then I need to add the record. 如果没有,那么我需要添加记录。 I am able to add successfully but I don't know how to update the record in LINQ. 我能够成功添加,但是我不知道如何在LINQ中更新记录。

Below is my attempt: 以下是我的尝试:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ReferralViewModel viewModel)
    {
        var candidateId = User.Identity.GetUserId();
         // I AM CONFUSED ABOUT BELOW STATEMENT
        var IsDupeReferral = _context.Referrals
       .Where(r => (r.CandidateId == candidateId)
      && (r.CompanyId == viewModel.CompanyId) && (r.SkillId == viewModel.SkillId))
      .Select(r=>r.ReferralId).SingleOrDefault();

        if(IsDupeReferral!=0)
        {
          //IF I FIND DUPE REFERRAL RECORD I WANT TO UPDATE SOME OF THE VALUES IN THAT
                  _context.Referrals.Where(r => r.ReferralId == IsDupeReferral).
             AND UPDATE r.resumeId with viewModel.ResumeId // How to do this?
             // NOT SURE ABOUT BELOW LINE EITHER
            _context.SaveChanges();
        }
        else
        {
           // BELOW CODE IS WORKING FINE
            var referral = new Referral
            {
                ReferralName = viewModel.ReferralName,
                ResumeId = viewModel.ResumeId,
                CandidateId = candidateId,
                DegreeId = viewModel.DegreeId,
                CoverLetterId = viewModel.CoverLetterId,
                SkillId = viewModel.SkillId
            };

            if (!string.IsNullOrEmpty(viewModel.TempCompany))
            {
                var newCompany = new Company
                {
                    CompanyName = viewModel.TempCompany
                };
                newCompany.Referrals.Add(referral);
                _context.Companies.Add(newCompany); ;
            }
            else
            {
                referral.CompanyId = viewModel.CompanyId.Value;
                _context.Referrals.Add(referral);
            }
            _context.SaveChanges();
        }

        return RedirectToAction("ReferralCenter");
    }
Referral referral = _context.Referrals.FirstOrDefault(r=> r.ReferralId = SomeId);
if(referral == null) // then referral does not exist - add it
{
    referral = new Referral{
                ReferralName = viewModel.ReferralName,
                ResumeId = viewModel.ResumeId,
                CandidateId = candidateId,
                DegreeId = viewModel.DegreeId,
                CoverLetterId = viewModel.CoverLetterId,
                SkillId = viewModel.SkillId
            };
    _context.Referrals.Add(referral);
}   
else  // referral already exists - update its values
{
    //make changes to referral
    referral.ReferralName = viewModel.ReferralName;
    referral.ResumeId = viewModel.ResumeId;
    referral.CandidateId = candidateId;
    referral.DegreeId = viewModel.DegreeId;
    referral.CoverLetterId = viewModel.CoverLetterId;
    referral.SkillId = viewModel.SkillId;

}
_context.SaveChanges(); //no matter added or updated - save the changes

Here's the solution 这是解决方案

//IF I FIND DUPE REFERRAL RECORD I WANT TO UPDATE SOME OF THE VALUES IN THAT
var referral = _context.Referrals.FirstOrDefault(r => r.ReferralId == IsDupeReferral);
// AND UPDATE r.resumeId with viewModel.ResumeId
if (referral !=null) {
    referral.resumeId = viewModel.ResumeId; 
    _context.Entry(referral).State = System.Data.EntityState.Modified;
    _context.SaveChanges();
}

Actually, you don't need getting the IsDupeReferral and then request the record again. 实际上,您不需要获取IsDupeReferral,然后再次请求记录。 Try to combine your code as the following: 尝试按以下方式合并您的代码:

var referral = _context.Referrals
       .Where(r => (r.CandidateId == candidateId)
      && (r.CompanyId == viewModel.CompanyId) && (r.SkillId == viewModel.SkillId)).SingleOrDefault();

    if (referral !=null) {
        referral.resumeId = viewModel.ResumeId; 
        _context.Entry(referral).State = System.Data.EntityState.Modified;
        _context.SaveChanges();
    }
    else {

     // add a new record 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Linq或Lambda执行更新?(C#,Asp.net,Linq,Lambda) - How to perform an update using Linq or Lambda?(C#, Asp.net,Linq , Lambda) 在 sql asp.net mvc 5 中使用 linq 时如何格式化记录中的数据? - How to format your data on a record when using linq in sql asp.net mvc 5? 如何在asp.net mvc中使用Linq从数据库中删除多条记录 - how delete more than one record from database using Linq in asp.net mvc 在asp.net mvc中删除记录后如何更新视图 - How to update view after deleting record in asp.net mvc 如何使用 LINQ 表达式 ASP.NET MVC Entity Framework 使用外键更新多个表 - How to Update multiple tables with foreign keys using LINQ expression ASP.NET MVC Entity Framework 如何在ASP.NET MVC 4 Web应用程序中使用LINQ更新两个表 - How to Update Two Tables using LINQ in ASP.NET MVC 4 Web Applications linq / lambda中的Asp.net mvc html.DisplayFor语法 - Asp.net mvc html.DisplayFor syntax in linq/lambda 如何使用ASP.NET MVC和KnockoutJS很好地添加记录? - How to add record well using ASP.NET MVC and KnockoutJS? 如何使用viewmodels插入记录:Asp.Net Mvc - How to insert record using viewmodels : Asp.Net Mvc 如何在asp.net mvc中使用下拉列表在数据库中插入记录? - How to Insert a record in database using dropdownlist In asp.net mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM