c# - Is there something I can do to optimise my linq query? -


I have to be able to solve a list of objects, which can double with some criteria so far code work But my list takes 10 minutes to 50,000 rows.

Here is the code:

  Public class TestObject {public string value1; Public string value 2; Public string value 3; Public string value 4; Public Entry Number 1; Public Entry Number 2; } Public static list & lt; TestObject & gt; ReturnTest object listWithoot double (list & lieutenant; test object & gt; source) {var destination = new list & lt; Test Object & gt; (); Var List = New Dictionary & lt; Int, TestObject & gt; (); While (source.count> gt; 0) {list.Clear (); Var Basic Elelection = Source [0]; Forresh (Select various queries in the source. (Element, Index) = & gt; New {value = element, Index = Index}). Where (current element => (present element.value.value1 == original element.value1) & amp; amp; (present element.value.value == original element.value 2) & amp; amp; (Current Element Value.value = 3 Original Element.valuere 3) & amp; amp; (Current Element.value.value == Original Element.value 4) {List. Add (query.Index, query.Value); } If (list.Count> 1) {Origin Element.num1 = list.Sum (a => a.Value.num1); Original Element.num2 = list.Sum (a => one.value. Nm2); } Destination. Add (basic element); Forresh (Work Position Torumov K.A.) list in the source. Revovat (Position Ttermov); } Return destination; }   

The idea is that when I pass the loop, then the list is reduced every time, so my linux request is probably executed on the smallest list. Although I have less than double, it is slow. I am looking for a solution that allows me to make the smallest routine possible, memory is not a problem.

Is anyone an idea?

I tried to follow through your code - and it seems that you just Want to remove a duplicate from the source list?

If this is the case:

  • Then I think your source.RemoveAt code could be broken because it removed the wrong element Could.

  • Then you should be able to run a group operation () on this source list - which should work using hashtable, which is what your existing loops-inside-loops Is much faster than operating New TestObject () grouped in

      var from new query in source group = value1 = s.value1, value2 = s.value2, value3 = s.value3, value4 = s.value4} {Value1 = Grouped. K. AW 1, value 2 = Grouped. K. AW2, value 3 = Grouped. K.value 3, value 4 = grouped. K. AW 4, Number 1 = Grouped. (X => X.num1), num2 = Grouped. Sum (x => x.num2)}; Return query. Toolist ();      

Comments