简体   繁体   English

C#无法使用LINQ更新记录

[英]C# Trouble updating record with LINQ

    public static bool CheckLogin(string Username, string Password, bool AutoLogin)
    {
        bool LoginSuccessful;

        // Trim inputs and verify lengths
        Username = Username.Trim();
        Password = Password.Trim().ToLower();

        // Get the associated user records
        DataClassesDataContext db = new DataClassesDataContext();
        var q = (from User in db.tblForumAuthors where User.Username == Username select new
                {
                    User.Password,
                    User.Salt,
                    User.Username,
                    User.Author_ID,
                    User.User_code,
                    User.Active,
                    User.Login_attempt,
                    User.Last_visit,
                }).SingleOrDefault();

        // Invalid details passed
        if (q == null)
        {
            LoginSuccessful = false;
        }
        else
        {
            // Increment login attempts counter
            int LoginAttempts = q.Login_attempt;
            LoginAttempts++;

            // Encrypt the password
            string HashedPassword = GetSha1(Password + q.Salt);

            // Check passwords match
            if (q.Password == HashedPassword)
            {
                LoginSuccessful = true;
            }
            else
            {
                LoginSuccessful = false;

                // Increment login attempts
                q.Login_attempt = LoginAttempts;
                db.SubmitChanges();
            }
        }
        return LoginSuccessful;
    }
}

On the line 在线上

q.Login_attempt = LoginAttempts;

I get: 我得到:

Error 50 Property or indexer 'AnonymousType#1.Login_attempt' cannot be assigned to -- it is read only C:\\inetpub\\wwwroot\\ScirraNew\\App_Code\\Login.cs 82 17 C:\\...\\ScirraNew\\

Can anyone show me how I can update this counter in the record please? 谁能告诉我如何更新记录中的此计数器?

You need to just select the whole User item if you want to edit it. 如果要编辑整个User项目,则只需选择整个User项目。 Get rid of your whole "Select New" clause. 摆脱整个“ Select New”子句。

You can't update properties of an anonymous class instance (even if you could that change just would be local and not be related to any User entity in the DB since the anonymous class instance is a new and different object that you are projecting to), you have to select the User entity itself to update. 您无法更新匿名类实例的属性(即使您所做的更改只是本地的,并且与数据库中的任何User实体都不相关,因为匿名类实例是您要投影的新对象) ,则必须选择User实体本身进行更新。

So instead of: 所以代替:

var q = (from User in db.tblForumAuthors
            where User.Username == Username
            select new
            {
                User.Password,
                User.Salt,
                User.Username,
                User.Author_ID,
                User.User_code,
                User.Active,
                User.Login_attempt,
                User.Last_visit,
            }).SingleOrDefault();

Use: 采用:

var q = (from User in db.tblForumAuthors where User.Username == Username select User).SingleOrDefault();

Or in lambda syntax: 或使用lambda语法:

var user =  db.tblForumAuthors.Where( u => u.UserName == UserName)
              .SingleOrDefault();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM