I would like to say this was working and now it is not. I don't know what would have changed.
var inquiry = repo.GetByInstanceId(2);
foreach (var s in inquiry.Prop)
{
s.Prop = "Test 1 2 3...";
}
Assert.AreEqual(true, inquiry.S.Single().IsDirty, "S Should Be Dirty");
What is happening is before the Property object would get updated and everything would be grand. Now the Property is being updated within the foreach loop but after the foreach loop it's like I didn't do anything.
--EDIT-- Okay, I thinks IsDirty is throwing everyone off. I wanted to put the exact code, minus a few name changes. But IsDirty is not important here.
A basic representation of what I am doing is this:
class test { public int check { get; set; } }
var x = new List<test>();
x.Add(new test(){ check = 1});
foreach (var y in x)
{
y.check = 5;
}
the Question is "Shouldn't this work?" I think it should, if not why? I had this working and I don't know what I have changed. I could show you my objects but I would have to modify the names so much it would be pointless. IsDirty isn't the problem. The value I am changing is changed inside the loop but not changed outside the loop.
-
2welcome to stackoverflow. Its really hard to know what you are asking without the context of your application.Daniel A. White– Daniel A. White2011年04月04日 18:51:49 +00:00Commented Apr 4, 2011 at 18:51
-
We need to see the code that sets the Prop and IsDirty properties.Emond– Emond2011年04月04日 18:53:02 +00:00Commented Apr 4, 2011 at 18:53
-
I have updated the question. I hope that is betterClaytonHunt– ClaytonHunt2011年04月04日 19:08:04 +00:00Commented Apr 4, 2011 at 19:08
-
1In the foreach loop y is a local variable that is assigned a value from the List setting y to a different value does not change the list. However that is not the same as your original post where a property is set. In short we need to see the code for Prop and IsDirty.juharr– juharr2011年04月04日 19:10:23 +00:00Commented Apr 4, 2011 at 19:10
-
what is the difference between a y and a property, should the property work?? I need to and was modifying the item from inquiry within the foreach loop using the iteratorClaytonHunt– ClaytonHunt2011年04月04日 19:12:33 +00:00Commented Apr 4, 2011 at 19:12
2 Answers 2
In your added explanation you create a copy in y and modify the copy.
This happens when the type of the list items is a ValueType
8 Comments
I think your problem might be involved with GetByInstanceId. If that is returning an IEnumerable then it is likely that your foreach is running through the values and making the change, but then your call to Single is reiterating and losing your changes. You might want to do a ToList after GetByInstanceId. Of course this is base on the assumption that inquiry.S.Single().IsDirty is a typo and should be inquiry.Single().IsDirty. Otherwise we need more info.