Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading