对象集合与LINQ的更新酒店 [英] Update property in object collection with linq
问题描述
有如下
公共类计划
{
与对象结构对象的列表公众诠释ID {搞定;组; }
公共字符串名称{;组; }
公共时间表(){}
}
执行数据LINQ查询,我可以看到的物体的正确填充列表
VAR进度=(从数据
D选择新计划
{
ID = d.id,
NAME =
})了ToList()。
在后面的代码我想改变属性名称取决于一个条件。举几个例子,我发现
schedule.ForEach(S = GT; {s.Name =条件名称;});
schedule.Select(S = GT; {s.Name =条件名称;返回小号;});
在执行休假名称参数空当我在监视窗口刷新时间表。
任何人都可以看看有什么不对的?
通过收集循环,并试图改变属性没有改变,要么
的foreach(附表VAR SCH)
{
sch.Name =新名;
}
schedule.ToList()[0] .Name点==在下面的代码片段p>
更新
.ToList()调用是非常重要的,使代码工作。
VAR进度=(从数据
D选择新的时间表
{
n = d.id,
名称=
})了ToList();
代码工作 - 执行schedule.ForEach后() Name属性将被更新。也许有一些东西,你已经离开了。
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
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
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?
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 == ""
UPDATE
.ToList() call in the snippet below is very important and makes code working.
var schedule = (from d in data
select new Schedule
{
ID = d.id,
Name = ""
}).ToList();
The code works - after executing schedule.ForEach() the Name property is updated. Maybe there is something that you've left out.
这篇关于对象集合与LINQ的更新酒店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!