Skip to content

Instantly share code, notes, and snippets.

@kyle-seongwoo-jun
Created December 28, 2018 08:44
Show Gist options
  • Save kyle-seongwoo-jun/62044b70326cec3fd69b8ceec7cdb0e7 to your computer and use it in GitHub Desktop.
Save kyle-seongwoo-jun/62044b70326cec3fd69b8ceec7cdb0e7 to your computer and use it in GitHub Desktop.
C# Linq ToList()
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