开发者

c# Linq can't update record

开发者 https://www.devze.com 2023-02-25 03:30 出处:网络
I\'m trying to update a record with LINQ, but get this error: Property or indexer \'AnonymousType#1.Comment\' cannot be assigned to -- it is read only

I'm trying to update a record with LINQ, but get this error:

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

This is thrown on the lines:

// Update parent comment
q.Comment = EditedText;
q.LastEdit = DateTime.Now;

Full class is below:

/// <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;
}


The error throws because you do a select new { c.UserID, c.PostDate, c.Comment, c.LastEdit }. if you do a select c, your code should work.

new {...} gives you an anonymous type, which is not updateable.


according to the error message q is an anonymous type ...

 var q = (from c in db.tblComments where c.ID == CommentID select new { c.UserID, c.PostDate, c.Comment, c.LastEdit }).Single();

you don't want to update that object ... update the object referenced by c in the LINQ statement (select it)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号