Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 30521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:27:39+00:00 2026-05-10T13:27:39+00:00

I love list comprehensions in Python, because they concisely represent a transformation of a

  • 0

I love list comprehensions in Python, because they concisely represent a transformation of a list.

However, in other languages, I frequently find myself writing something along the lines of:

foreach (int x in intArray)   if (x > 3) //generic condition on x     x++      //do other processing 

This example is in C#, where I’m under the impression LINQ can help with this, but is there some common programming construct which can replace this slightly less-than-elegant solution? Perhaps a data structure I’m not considering?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:27:40+00:00Added an answer on May 10, 2026 at 1:27 pm

    The increment in the original foreach loop will not affect the contents of the array, the only way to do this remains a for loop:

    for(int i = 0; i < intArray.Length; ++i) {     if(intArray[i] > 3) ++intArray[i]; } 

    Linq is not intended to modify existing collections or sequences. It creates new sequences based on existing ones. It is possible to achieve the above code using Linq, though it is slightly against its purposes:

    var newArray1 = from i in intArray select ((i > 3) ? (i + 1) : (i)); var newArray2 = intArray.Select(i => (i > 3) ? (i + 1) : (i)); 

    Using where (or equivalent), as shown in some of the other answers, will exclude any values less than or equal to 3 from the resulting sequence.

    var intArray = new int[] { 10, 1, 20, 2 }; var newArray = from i in intArray where i > 3 select i + 1; // newArray == { 11, 21 } 

    There is a ForEach method on arrays that will allow you to use a lambda function instead of a foreach block, though for anything more than a method call I would stick with foreach.

    intArray.ForEach(i => DoSomething(i)); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 142k
  • Answers 142k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See: System.Environment.SpecialFolder.Recent So: DirectoryInfo d = new DirectoryInfo( System.Environment.GetFolderPath(Environment.SpecialFolder.Recent) ); May 12, 2026 at 8:17 am
  • Editorial Team
    Editorial Team added an answer SELECT (SELECT id FROM YourTable WHERE id < YourId ORDER… May 12, 2026 at 8:17 am
  • Editorial Team
    Editorial Team added an answer This is just a shortcoming in the Visual Studio HTML… May 12, 2026 at 8:17 am

Related Questions

I love how in python I can do something like: points = [] for
I love the Remove unused usings command in VS2008, however when running it on
I'm trying to come up with an elegant way of creating a list from
I'm looking for a perfect text editor :) The must have list: vim-like modal

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.