Created
December 28, 2018 08:44
-
-
Save kyle-seongwoo-jun/62044b70326cec3fd69b8ceec7cdb0e7 to your computer and use it in GitHub Desktop.
C# Linq ToList()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var originArray = Enumerable.Range(1, 10).ToList(); | |
IEnumerable<int> subArray = originArray.Where(x => x != 0); // works with originArray | |
IEnumerable<int> subArray = originArray.Where(x => x != 0).ToList(); // creates new array | |
while (subArray.Count() > 0) | |
{ | |
var item = subArray.First(); | |
originArray.Remove(item); | |
Console.WriteLine(item); | |
if (item < 8) { | |
subArray = subArray.Skip(1); | |
continue; | |
} | |
Console.WriteLine($"pick {item}"); | |
break; | |
} | |
// output (without ToList()) | |
// 1 | |
// 3 | |
// 5 | |
// 7 | |
// 9 | |
// pick 9 | |
// output (with ToList()) | |
// 1 | |
// 2 | |
// 3 | |
// 4 | |
// 5 | |
// 6 | |
// 7 | |
// 8 | |
// pick 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment