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 6545513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:35:41+00:00 2026-05-25T11:35:41+00:00

I have a List/IEnumerable of objects and I’d like to perform a calculation on

  • 0

I have a List/IEnumerable of objects and I’d like to perform a calculation on some of them.

e.g.
myList.Where(f=>f.Calculate==true).Calculate();

to update myList, based on the Where clause, so that the required calulcation is performed and the entire list updated as appropriate.

The list contains “lines” where an amount is either in Month1, Month2, Month3…Month12, Year1, Year2, Year3-5 or “Long Term”
Most lines are fixed and always fall into one of these months, but some “lines” are calulcated based upon their “Maturity Date”.

Oh, and just to complicate things! the list (at the moment) is of an anonymous type from a couple of linq queries. I could make it a concrete class if required though, but I’d prefer not to if I can avoid it.

So, I’d like to call a method that works on only the calculated lines, and puts the correct amount into the correct “month”.
I’m not worried about the calculation logic, but rather how to get this into an easily readable method that updates the list without, ideally, returning a new list.

[Is it possible to write a lambda extension method to do both the calculation AND the where – or is this overkill anyway as Where() already exists?]

  • 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-05-25T11:35:41+00:00Added an answer on May 25, 2026 at 11:35 am

    Personally, if you want to update the list in place, I would just use a simple loop. It will be much simpler to follow and maintain:

    for (int i=0;i<list.Count;++i)
    {
         if (list[i].ShouldCalculate)
             list[i] = list[i].Calculate();
    }
    

    This, at least, is much more obvious that it’s going to update. LINQ has the expectation of performing a query, not mutating the data.


    If you really want to use LINQ for this, you can – but it will still require a copy if you want to have a List<T> as your results:

    myList = myList.Select(f => f.ShouldCalculate ? f.Calculate() : f).ToList();
    

    This would call your Calculate() method as needed, and copy the original when not needed. It does require a copy to create a new List<T>, though, as you mentioned that was a requirement (in comments).

    However, my personal preference would still be to use a loop in this case. I find the intent much more clear – plus, you avoid the unnecessary copy operation.


    Edit #2:

    Given this comment:

    Oh, and just to complicate things! the list (at the moment) is of an anonymous type from a couple of linq queries

    If you really want to use LINQ style syntax, I would recommend just not calling ToList() on your original queries. If you leave them in their original, IEnumerable<T> form, you can easily do my second option above, but on the original query:

    var myList = query.Select(f => f.ShouldCalculate ? f.Calculate() : f).ToList();
    

    This has the advantage of only constructing the list one time, and preventing the copy, as the original sequence will not get evaluated until this operation.

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

Sidebar

Related Questions

I have a list of objects contained in an IEnumerable<>. I would like to
If I have a list of objects IEnumerable<MyType> myTypes; Is it possible for me
Say I have: public ViewResult List() { IEnumerable<IModel> myList = repository.GetMyList(); if(1 == myList.Count())
I have an IEnumerable list of objects in C#. I can use a for
i have a list of project objects: IEnumerable<Project> projects a Project class as a
I have some code that creates objects in a foreach loop. So: private IEnumerable<StrongTypeResult>
I have a model that contains an IEnumerable of a list of custom objects.
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have the following code: List<T> list = new List<T>(); IEnumerable<T> query1 = ...

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.