使用LINQ ForEach设置列表属性值

时间:2015-01-02 16:46:45

标签: c# linq

我在linq查询下使用设置SecKey的值,但我仍然看到列表studentData中的旧值,

下面是我正在使用的样本,它不起作用/没有设置值,

studentData.Where(w => w.ActiveDate.Date.Equals(otherObject.ActiveDate.Date) && w.EndDate.Date.Equals(otherObject.EndDate.Date)).ToList().ForEach(s => s.SecKey = secKey);

这里我编写了一些数据,

public struct Student
{
    public string BadgeNum;
    public DateTime ActiveDate;
    public DateTime EndDate;
    public decimal Amount;
    public decimal? SecKey;
}


List<Student> students = new List<Student>();

students.Add(new Student() {
 BadgeNum = "1"
 ,
 ActiveDate = new DateTime(2014,4,4)
 ,
 EndDate = new DateTime(2014, 5, 6)
 ,
 Amount = 10
 ,
 SecKey = 1
});

students.Add(new Student()
{
    BadgeNum = "1"
    ,
    ActiveDate = new DateTime(2014, 5, 6)
    ,
    EndDate = new DateTime(2014, 5, 9)
    ,
    Amount = 10
    ,
    SecKey = 1
});

students.Add(new Student()
{
    BadgeNum = "1"
    ,
    ActiveDate = new DateTime(2014, 5, 9)
    ,
    EndDate = new DateTime(2014, 6, 6)
    ,
    Amount = 10
    ,
    SecKey = 1
});

foreach (var b in students)
{

    if (b.ActiveDate.Date.Equals(new DateTime(2014, 5, 9)) && b.EndDate.Date.Equals(new DateTime(2014, 6, 6)))
    {
        b.SecKey = 1;
    }
}

3 个答案:

答案 0 :(得分:2)

您有一个可变的值类型,因此当您重复对集合进行迭代时,您将复制所有项目并改变该副本,从而保持列表中的值不变。

如果您想在此处继续使用值类型,则需要将变异值分配回列表中的相应位置。

实际上,你根本不应该在这里使用值类型,特别是应该避免使用可变值类型,部分是出于这种情况的原因,你最终会在不知不觉中改变值的副本你打算改变。最好的解决方案是简单地将类型更改为class

答案 1 :(得分:1)

如果您想在不创建新列表的情况下实现目标,请尝试以下操作:

foreach (var student in studentData.Where(w => w.ActiveDate.Date.Equals(otherObject.ActiveDate.Date) && w.EndDate.Date.Equals(otherObject.EndDate.Date)))
{
student.SecKey = secKey;
}

编辑:这是在假设学生是class而不是struct的情况下创建的。

答案 2 :(得分:0)

在查询中执行ToList后,它将返回新列表。

var newlist = studentData.Where(w => w.ActiveDate.Date.Equals(otherObject.ActiveDate.Date) && w.EndDate.Date.Equals(otherObject.EndDate.Date)).ToList();

newlist.ForEach(s => s.SecKey = secKey);