Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ: How to Use RemoveAll without using For loop with Array
    primarykey
    data
    text
    <p>I currently have a log object I'd like to remove objects from, based on a LINQ query. I would like to remove all records in the log if the sum of the versions within a program are greater than 60. Currently I'm pretty confident that this'll work, but it seems kludgy:</p> <pre><code> for (int index = 0; index &lt; 4; index++) { Log.RemoveAll(log =&gt; (log.Program[index].Version[0].Value + log.Program[index].Version[1].Value + log.Program[index].Version[2].Value ) &gt; 60); } </code></pre> <p>The Program is an array of 4 values and version has an array of 3 values. Is there a more simple way to do this RemoveAll in LINQ without using the for loop? </p> <p>Thanks for any help in advance!</p> <hr> <p><strong>EDIT:</strong> Unfortunately the type of variable that Program and Version are based off of (which is a constraint of the framework I'm working in) restricts us such that I cannot access the "Any" member. I however confirmed that tzaman's solution works if you have <em>lists</em> by creating some sample code. I'm restricted to array-like variables (see the commented out areas) </p> <pre><code>// I'm restricted to Arrays, but if I had lists, this would work. internal class MyLogCollection { List&lt;MyLog&gt; MyListOfZones = new List&lt;MyLog&gt;(); public void TestRemove() { // Original Implementation for (int i = 0; i &lt; 4; i++) { MyListOfZones.RemoveAll(log =&gt; (log.MyZoneArray[0].MyVersionArray[0].Value + log.MyZoneArray[0].MyVersionArray[1].Value + log.MyZoneArray[0].MyVersionArray[2].Value) &gt; 60); //"Any" method is not available off of intellisense scope on MyZoneArray } // Better Implementation (thanks tzaman!) MyListOfZones.RemoveAll(log =&gt; (log.MyZoneArray.Any(prog =&gt; prog.MyVersionArray.Sum(ver =&gt; ver.Value) &gt; 60))); } } internal class MyLog { //public MyZone[] MyZoneArray = new MyZone[4]; public List&lt;MyZone&gt; MyZoneArray = new List&lt;MyZone&gt;(4); } internal class MyZone { //public MyVersion[] MyVersionArray = new MyVersion[3]; public List&lt;MyVersion&gt; MyVersionArray = new List&lt;MyVersion&gt;(3); } internal class MyVersion { public byte Value { get; set;} } </code></pre> <p>Thanks tzaman!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload