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

  • SEARCH
  • Home
  • 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 8081293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:41:08+00:00 2026-06-05T16:41:08+00:00

I am attempting to lean linq by replacing existing code in a project with

  • 0

I am attempting to lean linq by replacing existing code in a project with linq calls. In this method I check for a condition in my list of lines and if the condition is true, move that element from lines to processedLines.

The data structures are just lists:

List<LineSegment2> lines;
List<LineSegment2> processedLines;

The original code was:

for (int i = lines.Count - 1; i >= 0; i--)
{           
   if (lines[i].P2.x < sweepPosition)
   {
      processedLines.Add(lines[i]);
      lines.RemoveAt(i);
   }
}

and my linq code is:

var toMove = lines.FindAll(x => x.P2.x < sweepPosition);
toMove.ForEach(x =>
{
   processedLines.Add(x);
   lines.Remove(x);
});

My question is: Is this linq code less efficient because it is using more memory creating the temporary list ‘toMove’. Is there a way to create the linq query without requiring the temporary list or is the original code always more efficient?

  • 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. Editorial Team
    Editorial Team
    2026-06-05T16:41:10+00:00Added an answer on June 5, 2026 at 4:41 pm

    A more LINQy solution would be to add all the processed lines at once, then get the remaining lines:

    processedLines.AddRange(lines.Where(x => x.P2.x < sweepPosition));
    lines = lines.Where(x => x.P2.x >= sweepPosition).ToList();
    

    As for efficiency, it won’t be quite as fast as your original code. That’s not why you use LINQ.

    There is one potential advantage, though. It will make a new list of lines, so if you move a lot of lines to the processed list it will get rid of the unused items in the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to use the data series from this example no longer passes the JSONLint
Attempting/struggling to get registration and sign-up working within an active admin project. I have
Attempting to build a C# project which has numerous references to assemblies in NuGet
Attempting to deserialize a stream to List<T> (or any other type) and am failing
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
Attempting to compile the following code under Qt SDK 4.7.4 for Desktop - MinGW
Attempting to follow this Java tutorial . About 63 pages in, you are instructed
I'm attempting to gem install twitter-bootstrap-rails on my windows machine. This gem has a
Attempting to find printers / shares in Active Directory using C#. This is my
Attempting to upload a video, and this is the response I get back (after

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.