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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:52:24+00:00 2026-06-05T08:52:24+00:00

Can I update 2 fields using linq foreach loop at once? Sample snippet :

  • 0

Can I update 2 fields using linq foreach loop at once?
Sample snippet :
I have a userdata with Name, Email, CreateTime, LastUpdateTime fields.
I have to reset CreateTime and LastUpdateTime for all users.

To update i am using 2 calls as below

users.ForEach(x => x.CreateTime= DateTime.Now.AddMonths(-1));
users.ForEach(x => x.LastUpdateTime = DateTime.Now);

instead can I do it in a single using linq foreach loop?

  • 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-05T08:52:25+00:00Added an answer on June 5, 2026 at 8:52 am

    Well to start with, assuming this is List<T>.ForEach, this isn’t using LINQ. But yes, you can create a lambda expression using a statement body:

    users.ForEach(x => {
        x.CreateTime = DateTime.Now.AddMonths(-1);
        x.LastUpdateTime = DateTime.Now;
    });
    

    However, you may also want to use one consistent time for all the updates:

    DateTime updateTime = DateTime.Now;
    DateTime createTime = updateTime.AddMonths(-1);
    
    users.ForEach(x => {
        x.CreateTime = createTime;
        x.LastUpdateTime = updateTime;
    });
    

    It’s not really clear why you want to achieve it this way though. I would suggest using a foreach loop instead:

    DateTime updateTime = DateTime.Now;
    DateTime createTime = updateTime.AddMonths(-1);
    
    foreach (var user in users)
    {
        user.CreateTime = createTime;
        user.LastUpdateTime = updateTime;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with fields ID(primary key),Name and Address. how can i update
I have a strange problem. I am trying to update some fields using linq
How can I update the text fields with HTML? I have a page with
I'm generating ICalendar (.ics) files. Using the UID and SEQUENCE fields I can update
I am new to LINQ. I am using Visual Studio 2008 and have created
I am using UpdateListItems method (Lists.asmx web service) to update fields of an uploaded
Using C# and the Salesforce API, I can successfully update records, and I can
I am using LINQ to EF and have the following LINQ query: var results
I'm using Linq To Entities so I can Linq to my MySql database. My
I have tbl_names with following fields: Id int Name nvarchar(10) family nvarchar(20) Id Name

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.