简体   繁体   English

使用LINQ仅从表中获取所需的列以修改和保存记录会产生典型错误

[英]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 属性或索引器'匿名类型:string first_name,string last_name,string role,string email,bool account_locked.first_name'无法分配 - 它是只读的

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. 我们应该忘记小的效率,大约97%的时间说:过早的优化是所有邪恶的根源。 Yet we should not pass up our opportunities in that critical 3% - Donald Knuth 然而,我们不应该放弃那个关键的3%的机会 - 唐纳德克努特

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: 然后,你必须在你的linq查询中使用它:

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? 为了检索您可以编辑的内容,您必须创建一个类并在该类中选择您的实体,如下所示: 如何使用LINQ选择对象?

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. 注意:编辑此新对象时,您将无法调用context.SubmitChnages()。 You could do something like this though: LINQ to SQL: how to update the only field without retrieving whole entity 你可以这样做: LINQ to SQL:如何在不检索整个实体的情况下更新唯一的字段

This will allow you to update only certain parts of the object. 这将允许您仅更新对象的某些部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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