I have a Enumerable Where method, How to change it into Non-linq

so I have here a array of numbers to 75, and I’m showing it by a Linq’s where. 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;
        }

then here’s my linq where. Please help me to change it to Non-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));

>Solution :

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));

Leave a Reply