This question already has answers here:
Getting a collection of index values using a LINQ query
(6 answers)
Closed 9 years ago.
Background:
I'm working on an evaluator (I know there's solutions available, but I need some features that I need to implement myself). I need to find all occurrences of open brackets in the evaluation. However, for that I need all the indexes of the brackets.
Question:
Is there something like an AllIndexesOf method that returns a int[], or IEnumerable<int>?
There is not but you can get all the indexes using the following LINQ query.
int number = 10;
int[] intArray = new[] { 1, 32, 10, 5, 65, 6, 10, 10 };
var allIndexes = intArray.Select((r,i)=> new {value = r, index = i})
.Where(r=> r.value == number)
.Select(r=> r.index);
allIndexes will contain 2,6 and 7
You also can use Enumerable.Range
var indexes = Enumerable.Range(0, list.Count)
.Where(index => list[index] == yourValue);
Related
This question already has answers here:
Intersect two arrays
(2 answers)
Closed 5 months ago.
I've got 2 arrays of Int, and I want to keep only elements from second array that contains the first array elements.
int [] first = new int[2] { 1, 2};
int [] second = new int[5] { 99, 1, 2, 97, 95};
I have tried something like below.
foreach(int x in first){
second.Where(s=>s==x);
}
But it doesn't help me because I need to compare both elements from first array
second.Where(s=>s==x[0] && s[1])
and if the int is bigger I need. Do you have any ideas how to get below code line?
second.Where(s=>s==x[0] && s== x[1] && ... && s==x[n])
var elements = second.Where(first.Contains);
Maybe materialize it with a .ToList() or ToArray() call.
If the first list is really large, you could think about a faster version than the .Contains method, but for your lists, it would be overkill.
var firstSet = first.ToHashSet();
var result = second.Where(x => firstSet.Contains(x)).ToArray();
This question already has answers here:
Maintain subsequence order in OrderBy
(2 answers)
Closed 2 years ago.
Does OrderBy(item => false) affect array order?
I want to move certain element to the end of the array:
var numbers = new[] { 3, 7, 4, 1, 5 };
var result = numbers.OrderBy(item => item == 4).ToArray();
Does this solution have any side effects affecting the order of other elements (!=4)?
No this is fine, results with true (4) will be at the end of the list, your result will be 3, 7, 1, 5, 4
There is no side effects, as OrderBy operates on IEnumerable which doesn't have any methods to mutate collection
This question already has answers here:
Add zero-padding to a string
(6 answers)
C# convert int to string with padding zeros?
(15 answers)
Simple and elegant way to zero-pad a value in C# [duplicate]
(4 answers)
Closed 4 years ago.
So I Have a List, but the list is in integer.
Like 1, 2, 3, 4, 5,...,10,...100,..
And I want to convert that integer to string with format 00000X.
X is represent the integer. So the list will be 000001, 000002, 000010, 000100, etc.
you can achive this by using LINQ. Please check below answer.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 10, 100, 1000,10000,99999,100000 };
var mask = "00000";
List<string> stringNumbers = numbers.Select(x =>
{
if (mask.Length > x.ToString().Length)
{
return mask.Substring(0, mask.Length - x.ToString().Length) + x.ToString();
}
else return x.ToString();
}).ToList();
List<string> result = new List<string>();
foreach (int yourNumber in intList) {
result.Add("0000000".Substring(0, 7 - yourNumber.ToString().Length);
}
Sould work.
You also can do in on one line, intList.ForEach(...
This question already has answers here:
Getting odd/even part of a sequence with LINQ
(8 answers)
Closed 6 years ago.
How to split an array to 2 arrays with odd and even indices respectively? For example
int[] a = new int[]{1, 3, 7, 8};
then get two arrays
a1: {1, 7}
a2: {3, 8}
Simple using the overload of Where than contains the index which:
Filters a sequence of values based on a predicate. Each element's
index is used in the logic of the predicate function.
int[] a = new int[] { 1, 3, 7, 8 };
int[] aEven = a.Where((x, i) => i % 2 == 0).ToArray();
int[] aOdd = a.Where((x, i) => i % 2 != 0).ToArray();
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Populating a list of integers in .NET
Is there a simpler or more elegant way of initializing a list of integers in C# other than this?
List<int> numberList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
or
for(int i = 1; i <= 10; i++)
{
numberList.Add(i);
}
It just doesn't seem very practical - especially if the list was to contain a large number of values. Would a loop be a more practical solution?
Thanks,
CC
You can take advantage of the Enumerable.Range() method:
var numberList = Enumerable.Range(1, 10).ToList();
The first parameter is the integer to start at and the second parameter is how many sequential integers to include.
If your initialization list is as simple as a consecutive sequence of values from from to end, you can just say
var numbers = Enumerable.Range(from, end - from + 1)
.ToList();
If your initialization list is something a little more intricate that can be defined by a mapping f from int to int, you can say
var numbers = Enumerable.Range(from, end - from + 1)
.Select(n => f(n))
.ToList();
For example:
var primes = Enumerable.Range(1, 10)
.Select(n => Prime(n))
.ToList();
would generate the first ten primes assuming that Prime is a Func<int, int> that takes an int n and returns the nth prime.