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
Here is the code:
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;
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?
UPDATE
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.
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.
See more on this question at Stackoverflow