C# 使用 linq 更新对象集合中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18260994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update property in object collection with linq
提问by Maxim
Have a list of objects with the object structure as following
有一个对象列表,对象结构如下
public class Schedule
{
public int ID { get; set; }
public string Name { get; set; }
public Schedule() {}
}
Executing a linq query on data I can see properly populated list of objects
对数据执行 linq 查询我可以看到正确填充的对象列表
var schedule = (from d in data
select new Schedule
{
ID = d.id,
Name = ""
}).ToList();
later in code I want to change property Name depends on a condition. A few examples I found
稍后在代码中我想更改属性 Name 取决于条件。我发现的几个例子
schedule.ForEach(s => { s.Name = "Condition Name"; });
schedule.Select(s => { s.Name = "Condition name"; return s; });
after execution leave Name parameter "null" when I refresh schedule in the watch window. Can anyone see what's wrong with this?
执行后,当我在监视窗口中刷新计划时,将 Name 参数保留为“null”。任何人都可以看到这有什么问题吗?
Looping through collection and trying to change Property doesn't change it either
循环遍历集合并尝试更改属性也不会更改它
foreach (var sch in schedule)
{
sch.Name = "New name";
}
schedule.ToList()[0].Name == ""
schedule.ToList()[0].Name == ""
UPDATE
更新
.ToList() call in the snippet below is important to make code work.
下面代码段中的 .ToList() 调用对于使代码工作很重要。
var schedule = (from d in data
select new Schedule
{
ID = d.id,
Name = ""
}).ToList();
采纳答案by Slugart
The code works - after executing schedule.ForEach() the Name property is updated. Maybe there is something that you've left out.
代码有效 - 在执行 schedule.ForEach() 后,Name 属性被更新。也许有些东西你遗漏了。
回答by Tim Schmelter
LINQ is not the right tool to modify collections, it is a tool to query collections. If you want to modify it you need a loop, for example a foreach
:
LINQ 不是修改集合的正确工具,它是查询集合的工具。如果要修改它,则需要一个循环,例如 a foreach
:
var schedule = data.Select(d => new Schedule{ ID = d.id }).ToList();
foreach(var s in schedule)
s.Name = "Condition Name";
If you want to "modify" a collection with LINQ you have to create a new one and assign that to your variable which is inefficient.
如果您想使用 LINQ“修改”一个集合,您必须创建一个新集合并将其分配给您的变量,这是低效的。
回答by Jon
Your LINQ query that assigns a value to schedule
creates independent objects based on the original collection (it effectively clones the objects); changing the properties of the clones does not change the original objects.
您的 LINQ 查询分配一个值以schedule
根据原始集合创建独立对象(它有效地克隆了对象);更改克隆的属性不会更改原始对象。
回答by Khan
Below, you are create new Schedules
without setting its Name
.
下面,您将在Schedules
不设置其Name
.
var schedule = (from d in data
select new Schedule
{
ID = d.id
}).ToList();
To start, you may want to give the new Schedules
a Name
首先,您可能想要给新Schedules
的Name
var schedule = (from d in data
select new Schedule
{
ID = d.id,
Name = d.NameProperty /* Switch out with real name property */
}).ToList();