This is a migrated thread and some comments may be shown as answers.

Property or indexer cannot be assigned to -- it is read only

5 Answers 2865 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Duncan
Top achievements
Rank 2
Duncan asked on 01 Sep 2011, 05:52 AM
Hey there guys,

I have the following method with a LINQ join and i am getting the following error where trying to set some fields...

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


I'm obviously missing something. Any help would be great, I'm a little bit new to LINQ...

        public void setUser(UserObj userobj)
        {
            using (BluefireDBContext db = new BluefireDBContext(conn))
            {
                try
                {
                    var u = (from user in db.Users
                             join creds in db.Credentials on user.CredentialsID equals creds.CredentialsID
                             join acttype in db.AccountTypes on user.AccountTypeID equals acttype.AccountTypeID
                             join mobicar in db.MobiCarriers on creds.MobiCarrierID equals mobicar.MobiCarrierID
                             where creds.Email == userobj.Email
                             select new
                             {
                                 user.FirstName,
                                 user.LastName,
                                 creds.Email,
                                 creds.Password,
                                 creds.AuthPhone,
                                 mobicar.MailDomain,
                                 acttype.AcctTypeName,
                                 creds.IsLocked,
                                 creds.TemporaryToken,
                                 creds.TokenExpiration
                             }).Single();
 
 
 
                    if (u != null)
                    {
                                  u.FirstName = "";
...

5 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 02 Sep 2011, 04:13 PM
Hello Duncan,

 The following code used in the LINQ query generates an anonymous type. -
select new
{
 user.FirstName,
 user.LastName,
 creds.Email,
 creds.Password,
 creds.AuthPhone,
 mobicar.MailDomain,
 acttype.AcctTypeName,
 creds.IsLocked,
 creds.TemporaryToken,
 creds.TokenExpiration
})

Anonymous types encapsulate read-only properies. The exception is thrown since you attempt to set  a value of the proeprty. You should use a concrete type in order to modify the properties.

Do get back in case you need further assistance.

Kind regards,
Ady
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

0
Duncan
Top achievements
Rank 2
answered on 02 Sep 2011, 08:02 PM
I'm fairly new to LINQ and to OpenAccess ORM so please forgive my ignorance. All i am trying to do is JOIN a few tables and update the values of the records across these tables. A task i was familiar with with SQL.

Would you mind explaining to me the correct approach to doing a task like this?

Thanks you very much in advance.
0
Ady
Telerik team
answered on 07 Sep 2011, 09:16 PM
Hi Duncan,

Although this is something common with plain SQL, ORMs work a bit differently. Tables in the database are mapped to classes and all database interaction (reading and writing) is mostly done via these set of classes.
Performing a LINQ query that joins several types ( each type in turn is mapped to a table) results in a type that has fields (i.e columns) from several types clubbed together in an anonymous type. The ORM knows only about the persistent types defined in the domain model and can manage changes to these types.
You can select the types in the LINQ query and then update properties of the individual types.
For example:
var u = (from user in db.Users
         join acttype in db.AccountTypes on user.AccountTypeID equals acttype.AccountTypeID
         join creds in db.Credentials on user.CredentialsID equals creds.CredentialsID
         join mobicar in db.MobiCarriers on creds.MobiCarrierID equals mobicar.MobiCarrierID
         where creds.Email == userobj.Email
         select new
         {
             user,
             creds,
             mobicar,
             acttype,
           }).Single();
                                
u.user.FirstName = "change value";

Do get back in case you need further assistance


All the best,
Ady
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

0
Duncan
Top achievements
Rank 2
answered on 07 Sep 2011, 11:14 PM
Hi Ady,

I realize now that what i was doing in this post was incorrect. i opened up a new post in regards the the correct was on accessing the tables in a relationship with LINQ. http://www.telerik.com/community/forums/orm/linq-questions/461758.aspx

Of course, new questions have come up...
0
Lawrence
Top achievements
Rank 1
answered on 09 Oct 2017, 01:41 AM

Hi, Ady

If I want to use select to get a collection, then I can't use the Single() method, what should I do?

Tags
LINQ (LINQ specific questions)
Asked by
Duncan
Top achievements
Rank 2
Answers by
Ady
Telerik team
Duncan
Top achievements
Rank 2
Lawrence
Top achievements
Rank 1
Share this question
or