Home > Software design >  Using Linq's Where, Convert to Non-Linqs
Using Linq's Where, Convert to Non-Linqs

Time:11-07

So I have this code for my method a where clause.

playerhand[i] = numbers[0];
numbers = numbers.Where((source, index) => index != 0).ToArray();

Then so far I tried to make it a Non-Linq. I dont if I'm doing it right. Please correct me , there's no error but it displays a ton of zeros. I also refer to this : I have an Enumerable Where method, how to change it into non-linq , but mine is have a Source,index

playerhand[i] = numbers[0];
            for (int n = 0; n < 75 - i; n  )
        {
            if (playerhand[i] == numbers[n])
                numbers[n] = 0;
        }

CodePudding user response:

You seem to want to create a new array that omits the first value. So... do that? Perhaps the most frugal way is via a span:

numbers = numbers.AsSpan(1).ToArray();

or (earlier runtimes)

numbers = new Span<TheType>(numbers, 1, numbers.Length - 1).ToArray();

You could also just create the array in a temporary variable and use CopyTo

CodePudding user response:

If I truly understand your question, you want to do this:

numbers = numbers.Where((source, index) => index != 0).ToArray();

but without using linq. The code above only removes the first element of the array. With an array we have not Add or Remove methods So you can change your array to a list and easily remove the element you want:

List<int> numList = numbers.ToList();
numList.RemoveAt(0);
numbers = numList.ToArray();

Edit

This removes the first item without using linq:

for (int i = 0; i < numbers.Length - 1; i  )
{
       numbers[i] = numbers[i   1];
}
Array.Resize(ref numbers, numbers.Length - 1);

With the for loop we shift every number to its next one, and then after resize the array to remove the last item.

CodePudding user response:

EDIT

This answer makes the following assumptions:

  • The question is not about how to remove the first element of an array.

  • The question is about how to construct an equivalent of a Linq query using basic loop constructs.

So with this assumptions:

The query uses the following overload of the Where method:

public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);

where predicate is a Func<TSource,Int32,Boolean>

So it takes as a parameter a predicate, using both the current value and the index of the element of the collection which is enumerated.

A predicate is simply a function that returns a boolean, to be used for condition checking.

The following Linq query

numbers = numbers.Where((e, i) => predicate(e, i)).ToArray();

can be replaced by:

var result = new List<int>();

for (int i = 0; i < numbers.Length; i  )
{
    if( predicate(numbers[i], i) )
    {
        result.Add(numbers[i]);
    }
}

numbers = result.ToArray();

Indeed, when materialized, the Where query will do just that: take each element one by one, and check via a predicate if the element (and in this case its index) should be added to the newly constructed collection.

So in this specific case, the query would become:

var result = new List<int>();

for (int i = 0; i < numbers.Length; i  )
{
    if( i != 0 )
    {
        result.Add(numbers[i]);
    }
}

numbers = result.ToArray();

In this example, the predicate just uses the index, but generally it could use both the index and the current value.

  • Related