L
Logan S.
Using 3.5/SP1, and relatively new to LINQ:
In the following code you can see that I store a collection of objects from
which I later want to update a property of one of the objects.
I thought that .Single<T>(), as used in line 5, would give me a reference
to the object in the collection. But apparently it returns a complete copy
(new object) instead. I'm concluding this because when I update the property
(in line 6), the property of the object declared in line 1 is updated, but
the property of the object in the collection (allSnapInstances) is not
updated. It would be updated if the LINQ query were returning a reference to
that object.
Question: What am I missing? Is there some way I can use LINQ to get a
reference to the object in the collection rather than geting a new/copy? My
intent is to have line 6, below, set the property value on the instance of
the object stored in the collection, allSnapInstances.
1 SnapInfo currentSnapInfo = null;
2 List<SnapInfo> allSnapInstances = Session["m_AllSnapInstances"] as
List<SnapInfo>;
3 if (allSnapInstances != null)
4 {
5 currentSnapInfo = (from si in allSnapInstances
where si.AssociatedSiteComponent.SiteComponentID ==
currentSiteComponentID
select si).Single<SnapInfo>();
6 currentSnapInfo.SomeProperty = "some value";
7 }
Thanks!
In the following code you can see that I store a collection of objects from
which I later want to update a property of one of the objects.
I thought that .Single<T>(), as used in line 5, would give me a reference
to the object in the collection. But apparently it returns a complete copy
(new object) instead. I'm concluding this because when I update the property
(in line 6), the property of the object declared in line 1 is updated, but
the property of the object in the collection (allSnapInstances) is not
updated. It would be updated if the LINQ query were returning a reference to
that object.
Question: What am I missing? Is there some way I can use LINQ to get a
reference to the object in the collection rather than geting a new/copy? My
intent is to have line 6, below, set the property value on the instance of
the object stored in the collection, allSnapInstances.
1 SnapInfo currentSnapInfo = null;
2 List<SnapInfo> allSnapInstances = Session["m_AllSnapInstances"] as
List<SnapInfo>;
3 if (allSnapInstances != null)
4 {
5 currentSnapInfo = (from si in allSnapInstances
where si.AssociatedSiteComponent.SiteComponentID ==
currentSiteComponentID
select si).Single<SnapInfo>();
6 currentSnapInfo.SomeProperty = "some value";
7 }
Thanks!