Property Or Indexer 'anonymoustype#1.firstname' Cannot Be Assigned To -- It Is Read Only
Solution 1:
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.
Solution 2:
Anonymous types in C# are immutable; in other words, they cannot be changed. They don't have property set methods.
You can create a new anonymous type with the values if that helps.
Solution 3:
If your intent is to edit an entity and then save it back, you need to return the entity object(s) rather then an anonymous type that new {...} gives you:
ContactLINQDataContext context = new ContactLINQDataContext("Data Source=WORK-PC;Initial Catalog=Steve Harvey Team;Integrated Security=True");
var editContact = (from Contacts in context.Contacts
where Contacts.ContactID == contactID
select Contacts
}).First();
editContact.FirstName = firstNameBox1.Text;
Solution 4:
I found what I find the be the best solution to my problem. The main problem, as the other commenters have pointed out, is that the var type is an anonymous type. So then the question is, what type can I use? Well, I found that I can actually use my tables as a type, which works even better than a var. The following snippet is an example of a lambda LINQ query:
using (ContactLINQDataContextcontext=newContactLINQDataContext("Data Source=WORK-PC;Initial Catalog=Steve Harvey Team;Integrated Security=True"))
{
Contactcontact= context.Contacts.SingleOrDefault(x => x.ContactID == contactID);
Contactspouse= context.Contacts.SingleOrDefault(x => x.ContactID == contact.Spouse);
Propertyproperty= context.Properties.SingleOrDefault(x => x.PropertyID == contact.Address);
}
This runs 3 separate queries. A query in the contact table to get the contact row using the ContactID provided, a query in the contact table to get the spouse information using the ID in the Spouse column as the ContactID, and a query in the Property table using the info in the Property column of the initial Contact query for the Property ID.
Once these queries were executed, I was able to reference the data directly from the newly created variables.
using (ContactLINQDataContextcontext=newContactLINQDataContext("Data Source=WORK-PC;Initial Catalog=Steve Harvey Team;Integrated Security=True"))
{
Contactcontact= context.Contacts.SingleOrDefault(x => x.ContactID == contactID);
Contactspouse= context.Contacts.SingleOrDefault(x => x.ContactID == contact.Spouse);
Propertyproperty= context.Properties.SingleOrDefault(x => x.PropertyID == contact.Address);
firstNameBox1.Text = contact.FirstName;
lastNameBox1.Text = contact.LastName;
firstNameBox2.Text = spouse.FirstName;
lastNameBox2.Text = spouse.LastName;
streetBox.Text = property.Street;
cityBox.Text = property.City;
stateBox.Text = property.State;
zipBox.Text = property.ZIP;
phoneBox.Text = contact.Phone;
altPhoneBox.Text = spouse.Phone;
emailBox.Text = contact.Email;
}
And sure enough, it works like a charm. The text boxes were populated perfectly.
Post a Comment for "Property Or Indexer 'anonymoustype#1.firstname' Cannot Be Assigned To -- It Is Read Only"