c# - Updating object Linq'd from collection doesn't update the object in collection -


I have two simple classes with a UseCounter & lt; T & gt; and a simple container for other items (how many times it has been used and if it can be used once again) keeping some information about its use.

then dispenser & lt; T & gt; , where UseConter & lt; T & gt; Is a collection of and it is used to get the right elements from the collection and then update it.

I have a problem that the dispatcher's go () is in the law. This should return the object with the lowest combination of calculation and TotalCount , and then call the increase () method to increase the countdown needed.

However, when I run this code, dispenser & lt; T & gt; always gives the same element, it was not a reference to the object like Linq'd element, but its copy, hence increasing () method only increases the properties of the local object, But not one in the archives.

UseCounter:

P>

  Public square UseCounter & lt; T & gt; {Receive the public T element { Private set; } Public int TotalCount {get; Private set; } Public int count {get; Private set; } Go to Public Bull Useful; Set; } Public UserContent (T element) {this.Element = element; this. Number = 0; This total number = 0; this. Useful = true; } Public zero growth quarters () {this.Qount ++; This TotalCount ++; } Public Zero DecreaseCounter () {If (this.Count == 0) {New ArgumentOutRangeException Throw ("Calculation can not be less than 0!"); } This.Count--; }}  

Dispenser :

  Private Readonly Object _elementLocker = new object (); Private Inenimbable & lt; Username & lt; T & gt; & Gt; _elements; Public IEnumerable & lt; T & gt; Elements {Return _elments Choose (E = & gt; E.Element); }} Public antiphoncount {find {lock (_lamelalker) {returns _elements.Where (E => E. Euseil == incorrect). Content (); }}} Get public int current work {{lock (_elementLocker) {return _elements.Where (E => E.Count & gt; 0). }}} Public Dispenser (IEnumerable & lt; T & gt; Element) {this._elements = Elements Undoubtedly (). Choose (E => New UserContent & lt; T & gt; (E)); } Get Public T () {Lock (_elementLocker) {var lCount = _elements. Where (E => E. Eugel == true). Select (e = & gt; e.ount). Mean (); Var lTCount = _elements. Where (E => E. Eugel == true). Where (E => E.Count == lCount). Select (E = & E; E broad range). Mean (); Var L = _elements. Where (E => E. Eugel == true). First (e = & gt; e.kount == lCount & amp; amp; equals == lTCount); El.IncreaseCounter (); Return aliment; }} Public Zero Reports (T elements, successful hair) {lock (_elementLocker) {var el = _elements. First (E => E. element == element); El.DecreaseCounter (); If (el.Useful == true and successful == true) {el.Useful = false; }}}  

This problem is:

  This._ elements = element Undoubtedly (). Choose (E => New UserContent & lt; T & gt; (E));  

This is lazy every time you repeat on _elements , they will go back to the original source data, get a different set, and then the project that New Sequence UseCounter Examples Any changes made to those components are immutable when you iterate over the next _elements .

If you are happy to be "stable" on the set of input elements, you can only execute the query, so it can only be executed once:

< Pre> this._elements = element. Undoubtedly (). Selection (e = & gt; New UserContent & lt; T & gt; (e)) To create a list ();

Now every time you repeat on elements , you are running again on sequence similar to references.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -