ASP.NET / C# / LINQ: Where Greater

Is there a way to use LINQ instead of foreach here and to do everything in just one or two lines of code?
            var numbers = new List<double> { -3, -2, -1, 0, 1,2,3,4,5,6,7,8,9,10 };
            var numbers2 = new List<double> {};

            foreach (double nn in numbers)
            {
                   if(nn > 2) numbers2.Add(nn);
            }

            foreach (double nn in numbers2)
            {
                  Print(nn);
            }

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
var numbers2 = numbers.Where(number => number > 2);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Naitik GamitSoftware DeveloperCommented:
Use As,

var ObjNum=numbers.Where(n => n > 2);

numbers2.AddRange(ObjNum);

Print(numbers2);
0
Lokesh B RDeveloperCommented:
Hi,

Check this code

var numbers = new List<double> { -3, -2, -1, 0, 1,2,3,4,5,6,7,8,9,10 };
 var list = numbers.Where(n=> n > 2);

            foreach (var num in list)
            {
                   Print(num);
            }

Open in new window

0
käµfm³d 👽Commented:
I'm unsure why everyone feels compelled to repeat my answer, but if you truly want to nix all of the foreach loops, then you could do so by converting the result to a list, and then calling the ForEach method against that list:

var numbers = new List<double> { -3, -2, -1, 0, 1,2,3,4,5,6,7,8,9,10 };
var numbers2 = numbers.Where(number => number > 2)
                      .ToList()
                      .ForEach(Print);

Open in new window


Sure, the code still says "for each", but it's a tad bit tidier in its presentation. Just be certain you understand what's going on with that approach--you don't want to introduce code that causes more confusion to other developers, though in this particular case I wouldn't expect that.

Also, if you were to include the morelinq library from NuGet, then you could even get rid of the ToList call:

var numbers = new List<double> { -3, -2, -1, 0, 1,2,3,4,5,6,7,8,9,10 };
var numbers2 = numbers.Where(number => number > 2)
                      .ForEach(Print);

Open in new window

0
skijAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.