使用Linq查询更新无法正常工作

时间:2016-08-09 11:56:21

标签: c# sql linq

我是Linq查询的新手,我从多个文本框中获取我的用户参数来更新数据库中的特定用户,但它不能正常工作我尝试多种方式但是他们不能在那里工作没有错误但也没有更新用户

代码:

  return obj.job.job_type.name

2 个答案:

答案 0 :(得分:1)

您只需在执行select new时尝试更新投影。 相反,你应该去做这样的事情:

var query = (from p in dbcontext.Users
                 where p.UserId == person.UserId
                 select p).SingleOrDefault();

休息应该没问题。

*编辑:有关投影操作(C#)的更多信息以及更清晰的想法,请参阅:

https://msdn.microsoft.com/en-us/library/mt693038.aspx

答案 1 :(得分:0)

试试这种方式

您可以使用上下文对象和更新值来保存在数据库中。

    public void Update(UserEntity person)
        {
            UserEntity user = new UserEntity();

            SurveyEntities dbcontext = new SurveyEntities();

          //  var query = (from p in dbcontext.Users
          //               where p.UserId == person.UserId
         //                select new UserEntity() { UserId =p.UserId , FirstName = p.FirstName, LastName = p.LastName, Birth = p.Birth.Value, Password = p.Password, UserName = p.Username, Email = p.Email, Active = p.Active.Value }).SingleOrDefault();

  UserEntity tmp=  dbcontext.UserEntity.where(x=>x.userId==person.UserId).FirstDefault();
                tmp.FirstName = person.FirstName;
                tmp.LastName = person.LastName;
                tmp.UserName = person.UserName;
                tmp.Password = person.Password;
                tmp.Email = person.Email;
                tmp.Birth = person.Birth;
                tmp.Active = person.Active;


            try
            {
                dbcontext.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // Provide for exceptions.
            }


        }
相关问题