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

5 posts, 0 answers
  1. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 01 Sep 2011 Link to this post

    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 = "";
    ...
  2. Ady
    Admin
    Ady avatar
    588 posts

    Posted 02 Sep 2011 Link to this post

    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 >>

  3. DevCraft banner
  4. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 02 Sep 2011 Link to this post

    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.
  5. Ady
    Admin
    Ady avatar
    588 posts

    Posted 07 Sep 2011 Link to this post

    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 >>

  6. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 07 Sep 2011 Link to this post

    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...
Back to Top
DevCraft banner