Q: How to convert
Q: How to access
Q: How to use the
Q: How does the c
Laparoscopic diagn
The present invent
Bahrain’s Foreign
Newport Beach, Cal
A group of interna
Methotrexate-assoc

The present invent
Q: XPATH querying
Oil and water, yin
Methanotrophs colo
Q: How to set the
LOS ANGELES -- It'
We’re back! Join h
The present invent
Q: PHP date() fun
[A case of bilater
Q: How can I add additional values to a List based on an if statement? I am trying to understand how you would implement a function in C# that takes a List, and inserts additional values based on a given condition. So for example, if my original list contains the following values: 1, 4, 7, 5, 8, 9. I would like to take the code that runs in this if statement, and add it to a new List which is not the same as the original List. That new List should be the original list plus the numbers 1, 2, 3. I am basically trying to find the index of each value from the original List which is less than 7, and append a 1 and 2 in front of the existing numbers. So the new list would be 3, 4, 5, 6, 1, 2, 3, 7, 8, 9. I understand how to loop through the list, but do not understand how I can add values to a list at a specific index. In my code below I just get a null reference error for value = aList[i] and return value = newList[i]. Any help or feedback would be appreciated! public void addOne(List aList) { if (aList.Min() < 6) { //get values that need to be added to the list and //put in a new list with a one before each number. } return value; } A: It's better to make a new list of a List type: var newList = Enumerable.Range(1,9) .Where(x => aList.Any(y => y < 7)) .ToList(); You can use a for loop instead of Enumerable.Range and also use an array instead of List. A: Just adding to an existing list, rather than creating a new one, can be done with the Aggregate extension method. You can use: aList.Aggregate((current, next) => current + (current < 7 ? 1 : 0) + next); This requires C# 7, so you can either use Visual Studio 2017 or the newer .NET Core to compile this. You could also pass a custom delegate, as the name suggests, rather than a condition, you could write: aList.Aggregate(new int[] { 1, 2, 3 }, (current, next) => current.Concat(new [] { next })); where new int[] { 1, 2, 3 } is an IEnumerable representing the items to add to the list. That's simpler than an if condition. A: Here's a slightly shorter version of the list.Add(i) answer: int[] aList = { 1, 4, 7, 5, 8, 9 }; var result = aList.Concat(Enumerable.Repeat(1, 2)); The Concat method takes an array and repeats the elements in it n times. Note: The Repeater overload takes the count you want it to repeat the array. I used a 1 and 2 in this example. Also note that the ToList method is expensive when you don't use the return value. If you just need the enumerable then avoid ToList. If you want to turn your list into a new list of ints rather than a list of arrays then do: List newList = aList.Where(x => x < 7).ToList(); Where will just filter your list for you. It's simpler to read than the array version. In response to the comment below the question. If you need to add items after your code already has a reference to the list: int[] aList = { 1, 4, 7, 5, 8, 9 }; List newList = aList.Where(x => x < 7).ToList(); newList.Add(1); To access the items: foreach (var i in newList) { Console.WriteLine(i); } If you want to avoid the performance of ToList, then: int[] aList = { 1, 4, 7, 5, 8, 9 }; IEnumerable newList = aList.Where(x => x < 7); foreach (var i in newList) { Console.WriteLine(i); } Of course, in this case, you don't need a temporary array since you don't need to use the result, but in other situations you can do: int[] aList = { 1, 4, 7, 5, 8, 9 }; IEnumerable newList = aList.Where(x => x < 7).ToList(); foreach (var i in newList) { Console.WriteLine(i); } You can also use the ToList extension method. If you need to modify the result, but still have a reference to the original list: int[] aList = { 1, 4, 7, 5, 8, 9 }; var newList = aList.Where(x => x < 7).ToList(); newList.Add(1); If you need to add after the initial ToList call, you can use LINQ's ElementAt method to insert an element at a specific index. var newList = aList.ToList(); var count = 1; var items = aList.ElementAt(0)...; for (var i = count + 1; i < aList.Length; i++) { items.Add(aList[i]); }