无法将属性或索引器"AnonymousType#1.FirstName"分配给它-只读 [英] Property or indexer 'AnonymousType#1.FirstName' cannot be assigned to -- it is read only

查看:396
本文介绍了无法将属性或索引器"AnonymousType#1.FirstName"分配给它-只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个将查询结果值更改为文本框中的值的函数.我可以运行LINQ查询,获取值,并且读取可以完美打印...但是当我尝试将查询的值更改为文本框值时,出现以下错误:

I'm trying to make a function that changes the query result value to the values in a textbox. I can run the LINQ query, get the values, and read can print the perfectly... But when I try change the queried value to the textbox value, I get the following error:

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

这是代码:

    private void editQuery(int contactID)
    {
        ContactLINQDataContext context = new ContactLINQDataContext("Data Source=WORK-PC;Initial Catalog=Steve Harvey Team;Integrated Security=True");
        var editContact = (from Contacts in context.Contacts
                           join prop in context.Properties on Contacts.Address equals prop.PropertyID
                           join spouse in context.Contacts on Contacts.Spouse equals spouse.ContactID
                           where Contacts.ContactID == contactID
                           select new
                           {
                            ID = Contacts.ContactID,
                            FirstName = Contacts.FirstName,
                            LastName = Contacts.LastName,
                            FirstName2 = spouse.FirstName,
                            LastName2 = spouse.LastName,
                            Street = prop.Street,
                            City = prop.City,
                            State = prop.State,
                            ZIP = prop.ZIP,
                            Phone1 = Contacts.Phone,
                            Phone2 = Contacts.AltPhone,
                            Email = Contacts.Email,
                            ContactType = Contacts.Type,
                            Assets = Contacts.Assets,
                            Notes = Contacts.Notes,
                           }).First();

        editContact.FirstName = firstNameBox1.Text;

我认为可能是因为我试图更改var类型,但是我看不到如何更改类型并仍然更新值.有什么想法吗?

I thought it might be because I am trying to change a var type, but I don't see how I could change the type and still update the value. Any ideas?

更新

我找到了最终对我有用的解决方案.谢谢张贴者的答复,他们使我走上了正确的轨道.我在下面的评论中详细介绍了我的解决方案.

I found a solution that finally worked for me. Thank you posters who replied and set me on the right track. I detailed my solution in a comment below.

推荐答案

这是因为您正试图更改 anonymous 类型的值-不存在var类型的东西.这只是告诉编译器从赋值运算符右侧的表达式中推断出变量的类型.匿名类型中的属性始终是只读的.您需要创建另一个实例:

It's because you're trying to change the value of an anonymous type - there's not such thing as a var type; that's just telling the compiler to infer the type of the variable from the expression on the right-hand side of the assignment operator. The properties in anonymous types are always read-only. You'll need to create another instance:

editContact = new {
    editContact.Id,
    FirstName = firstNameBox1.Text,
    LastName = editContact.LastName,
    ...
};

请注意,您需要以相同的顺序指定所有具有相同类型的属性-这样,两个匿名类型创建表达式将具有相同的类型.

Note that you need to specify all the properties, with the same types, in the same order - that way the two anonymous type creation expressions will be of the same type.

这篇关于无法将属性或索引器"AnonymousType#1.FirstName"分配给它-只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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