Home > Software engineering >  I have an Enumerable Where method, how to change it into non-linq
I have an Enumerable Where method, how to change it into non-linq

Time:11-07

I have an array of numbers to 75, and I'm showing it by using a Linq where clause. I'm asking is to transfer a code of linq to non linq. Using a list ?

playerhand = new int[75];
numbers = new int[75];

for (int i = 0; i < 75; i  )
{
    sClass.numbers[i] = i   1;
}

Here's my linq where code snippet. Please help me to change it not to use any Linq:

Console.WriteLine("Player Hand : ");
Console.WriteLine("{0}", string.Join(", ", sClass.playerhand.Where(x => x != 0)));
Console.WriteLine("Bingo Numbers : ");
Console.WriteLine("{0}", string.Join(", ", numbers));

CodePudding user response:

You need somewhere to collect the items. Since we don't know how many items will match, we should use a List<T>:

List<int> values = new List<int>();

Then you need to loop through each item and add the matching ones to the list:

for (int i = 0; i < sClass.playerhand.Length;   i)
{
    if (sClass.playerhand[i] != 0)
    {
        values.Add(sClass.playerhand[i]);
    }
}

Then you can use the values list in place of your existing LINQ expression:

Console.WriteLine("{0}", string.Join(", ", values));
  • Related