修改LINQ输出AnonymousType不能分配 [英] Modify LINQ output AnonymousType cannot be assigned

查看:397
本文介绍了修改LINQ输出AnonymousType不能分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我一直都在黑客攻击它时我不断收到错误

  

属性或索引AnonymousType#1.XP'不能被分配​​到 - 它      仅读

该问题发生在a.XP这里

 的foreach(VAR一个注释中)
    {
        a.XP = score.getLevel(a.XP);
    }
 

和作为评论指出,我从来没有说我想要做什么,我想对子级替代a.XP与改进值score.getLevel(a.XP)。

下面是完整的code

 保护无效GetComments()
{
    TimberManiacsDataContext DB =新TimberManiacsDataContext();
    分数分数=新的分数();
    VAR评论=(从fComment在db.Comments
                    其中,fComment.PublishID == Convert.ToInt32(的Request.QueryString [文章])
                    排序依据fComment.DateTime降
                    选择新
                    {
                        用户名= fComment.User.UserLogin.Username,
                        发表时间= fComment.DateTime,
                        UserImage = fComment.User.UserGeneralInfo.ProfilePicture,
                        注释= fComment.Comment1,
                        用户名= fComment.UserID,
                        XP = fComment.User.CommunityScore
                    })采取(10).ToList();

    的foreach(在评论VAR一)
    {
        a.XP = score.getLevel(a.XP);
    }
    Commentlist.DataSource =意见;
    Commentlist.DataBind();
}
 

解决方案

匿名类型对象是只读的,但考虑到你的code样品,有一点需要尝试这种修改在循环,只是让一部分你的查询执行。

  XP = score.getLevel(fComment.User.CommunityScore)
 

如果你发现自己需要进行修改的查询执行后,那么你应该继续和定义一个类,允许这样的突变,然后选择成类而不是匿名类型的。

 类CommentData {...} //你的属性定义这种类型

//然后将其用于查询投影

选择新CommentData
{
    // ...
}
 

Hi i have a problem and ive been hacking at it for hours i keep getting the error

Property or indexer 'AnonymousType#1.XP' cannot be assigned to -- it is read only

The problem occurs on the a.XP here

    foreach (var a in comments)
    {
        a.XP = score.getLevel(a.XP);
    }

and as a comment pointed out i never say what i want done, i whould like to substitute a.XP with the improved value score.getLevel(a.XP).

Here is the full code

protected void GetComments()
{
    TimberManiacsDataContext db = new TimberManiacsDataContext();
    Score score = new Score();
    var comments = (from fComment in db.Comments
                    where fComment.PublishID == Convert.ToInt32(Request.QueryString["Article"])
                    orderby fComment.DateTime descending
                    select new
                    {
                        UserName = fComment.User.UserLogin.Username,
                        PostTime = fComment.DateTime,
                        UserImage = fComment.User.UserGeneralInfo.ProfilePicture,
                        Comment = fComment.Comment1,
                        UserID = fComment.UserID,
                        XP = fComment.User.CommunityScore
                    }).Take(10).ToList();

    foreach (var a in comments)
    {
        a.XP = score.getLevel(a.XP);
    }
    Commentlist.DataSource = comments;
    Commentlist.DataBind();
}

解决方案

Anonymously-typed objects are read only, but given your code sample, there is little need to try this modification in the loop, simply make it part of your query execution.

XP = score.getLevel(fComment.User.CommunityScore)

If you find yourself in need of performing changes after the query has executed, then you should go ahead and define a class that allows such mutations and then select into that class instead of the anonymous type.

class CommentData { ... } // define this type with your properties

// then use it for the query projection

select new CommentData 
{
    // ...
}

这篇关于修改LINQ输出AnonymousType不能分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆