.net - Adding arrays without copying duplicate values in C# -


what fastest way this:

var firstarray = enumerable.range(1, 10).toarray(); // 1,2,3,4,5,6,7,8,9,10 var secondarray = enumerable.range(9, 3).toarray(); //                 9,10,11,12 var thirdarray = enumerable.range(2, 3).toarray();  //   2,3,4,5 //add these arrays expected output            1,2,3,4,5,6,7,8,9,10,11,12 

is there linq way this. quite have huge list of array iterate. example

var firstarray = enumerable.range(1, 10).toarray(); // 1,2,3,4,5,6,7,8,9,10 var secondarray = enumerable.range(12, 1).toarray(); //                     12,13 //add these arrays expected output            1,2,3,4,5,6,7,8,9,10,12,13 

note: prefer function work on date ranges.

.union give distinct combination of various sequences. note: if working custom type, need provide overrides gethashcode/equals inside class or provide iequalitycomparer<t> type in overload. bcl types such int or datetime, fine.

example:

var sequence = enumerable.range(0,10).union(enumerable.range(5,10)); // should result in sequence of 0 through 14, no repeats 

edit

what elegant way union ranges without chaining them in 1 command.

if have sequence of sequences, collection of lists, perhaps jagged array, can use selectmany method along distinct.

int[][] numberarrays = new int[3][]; numberarrays[0] = new int[] { 1, 2, 3, 4, 5 }; numberarrays[1] = new int[] { 3, 4, 5, 6, 7 }; numberarrays[2] = new int[] { 2, 4, 6, 8, 10 };  var alluniquenumbers = numberarrays.selectmany(i => i).distinct(); 

otherwise, might consider creating own extension method handle this.

public static class myextensions {     public static ienumerable<t> unionmany<t>(this ienumerable<t> sequence, params ienumerable<t>[] others)     {         return sequence.union(others.selectmany(i => i));     } }  //  var alluniques = numberarrays[0].unionmany(numberarrays[1], numberarrays[2]); 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -