简体   繁体   中英

Fetching only required columns from table using LINQ to modify and save record gives a typical error

So what I was trying was to fetch only those columns from table which has to be updated and I tried it as below:

var user = (from u in context.tbl_user where u.e_Id == model.Id select 
           new { u.first_name, u.last_name, u.role, u.email, u.account_locked })
           .FirstOrDefault();

But when I tried to assign new value to the fetched data as below

user.first_name = model.FirstName;

I saw below error getting displayed

错误图片

Property or indexer 'anonymous type: string first_name, string last_name, string role, string email, bool account_locked.first_name' cannot be assigned to -- it is read only

But when I retrieved all the values from table without filtering as below it worked fine.

var user = (from u in context.tbl_user where u.e_Id == model.Id select u).FirstOrDefault();

Why it doesn't work for first query. I've read in many sites that it is good to retrieve only required properties from database in terms of performance and security. But I am really not able to understand what's wrong with the first approach I opted. Any explanations are much appreciated.


Update

Are there any other ways to fetch only required column and update them and store them back?

Anonymous Types properties are read-only so you can not change them.

Stop doing micro-optimizing or premature-optimization on your code. Try to write code that performs correctly, then if you face a performance problem later then profile your application and see where is the problem. If you have a piece of code which have performance problem due to finding the shortest and longest string then start to optimize this part.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3% - Donald Knuth

If you want to just fetch specific columns you can create a custom class and fill the properties in your query like others have mentioned.

As others said anonymous type is read only, for achieving what you want, you will have to create a type for it with properties that are required:

public class User
   {
       public string FirstName {get;set;}
       public string LastName {get;set;}
       .....................
       .....................
   }

and then, you have to use it in your linq query:

var user = (from u in context.tbl_user 
            where u.e_Id == model.Id
            select new User 
                  { 
                     FirstName = u.first_name,
                     LastName =  u.last_name,
                     ......................,
                     ..................... 
                   }).FirstOrDefault(); 

Anonymous types are read-only by design.

In order to retrieve something that you can edit, you have to create a class and select your entities into that class, as done here: How to use LINQ to select into an object?

Or here:

var user = (from u in context.tbl_user where u.e_Id == model.Id select 
           new User_Mini { u.first_name, u.last_name, u.role, u.email, u.account_locked })
           .FirstOrDefault();

Note: you won't be able to call context.SubmitChnages() when editing this new object. You could do something like this though: LINQ to SQL: how to update the only field without retrieving whole entity

This will allow you to update only certain parts of the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM