微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c#Linq无法更新记录

如何解决c#Linq无法更新记录

| 我正在尝试使用LINQ更新记录,但出现此错误
Property or indexer \'AnonymousType#1.Comment\' cannot be assigned to -- it is read only
Property or indexer \'AnonymousType#1.LastEdit\' cannot be assigned to -- it is read only
这是在网上抛出的:
// Update parent comment
q.Comment = EditedText;
q.LastEdit = DateTime.Now;
完整课程如下:
/// <summary>
/// Creates a new edit for a comment
/// </summary>
/// <param name=\"CommentID\">ID of comment we are editing</param>
/// <param name=\"EditedText\">New text for comment</param>
/// <param name=\"UserID\">ID of user making the edit</param>
/// <returns>Status</returns>
public static CommentError NewEdit(int CommentID,string EditedText,int UserID)
{
    CommentError Status = CommentError.UnspecifiedError;

    using (DataClassesDataContext db = new DataClassesDataContext())
    {
        bool IsOriginalAuthor = false;
        var q = (from c in db.tblComments where c.ID == CommentID select new { c.UserID,c.PostDate,c.Comment,c.LastEdit }).Single();
        if (q == null)
            Status = CommentError.UnspecifiedError;
        else
        {
            if (q.UserID == UserID)
                IsOriginalAuthor = true;

            // Check if they are within lock time
            bool CanEdit = true;
            if (IsOriginalAuthor)
            {
                if (q.PostDate.AddMinutes(Settings.MinsUntilCommentLockedFromEdits) > DateTime.Now)
                {
                    Status = CommentError.CommentNowUneditable;
                    CanEdit = false;
                }
            }
            // Passed all checks,create edit.
            if (CanEdit)
            {
                // Update parent comment
                q.Comment = EditedText;
                q.LastEdit = DateTime.Now;

                // New edit record
                tblCommentEdit NewEdit = new tblCommentEdit();
                NewEdit.CommentID = CommentID;
                NewEdit.Date = DateTime.Now;
                NewEdit.EditedText = EditedText;
                NewEdit.UserID = UserID;
                db.tblCommentEdits.InsertOnSubmit(NewEdit);



                db.SubmitChanges();
                Status = CommentError.Success;
            }
        }
    }
    return Status;
}
    

解决方法

因为您做了
select new { c.UserID,c.PostDate,c.Comment,c.LastEdit }
,所以引发错误。如果您执行
select c
,则您的代码应该可以正常工作。
new {...}
为您提供匿名类型,该类型不可更新。     ,根据错误消息q是匿名类型...
 var q = (from c in db.tblComments where c.ID == CommentID select new { c.UserID,c.LastEdit }).Single();
您不想更新该对象...更新LINQ语句中c引用的对象(选择它)     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?