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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:32:45+00:00 2026-06-08T05:32:45+00:00

Need help in following code segment: static void Main(string[] args) { var customers =

  • 0

Need help in following code segment:

static void Main(string[] args)
{
    var customers = new HashSet<Customer>();
    var action = new Action(() =>
    {
        var innerCustomers = new Customer[] 
        { 
            new Customer { CustomerID = 1, CustomerName = "C 1" }, 
            new Customer { CustomerID = 2, CustomerName = "C 2" }, 
        };

        innerCustomers.Select(c => customers.Add(c)); //doesn't work

        foreach (var customer in innerCustomers) 
            customers.Add(customer); //works fine
    });

    action();
}

innerCustomers.Select(c => customers.Add(c)); does not seem to be working in terms of inserting records in the “customers” collection however, “foreach” down below that line works fine. Anybody has any idea why it’s not working in linq? I know i am not selecting anything out of the select method

  • 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-08T05:32:47+00:00Added an answer on June 8, 2026 at 5:32 am

    You Select it, but don’t do anything with the result. Because of LINQ’s lazy evaluation (it generates the results on-demand through an IEnumerable) it just won’t get executed. Use the foreach loop, it’s the cleanest possible solution.

    (The other solution is to use a List<Customer> instead and call ForEach on that… but unless you have a really good reason for wanting to use a method with a callback, there’s no advantage.)

    Edit: Actually, if all you’re doing is adding elements to the HashSet, the cleanest possible solution is UnionWith:

    customers.UnionWith(innerCustomers);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new to java script and i need some help with following code
I am new to regular expressions and I need help condensing the following code:
I need syntax help with the following code logic: I have a code block
I need help concerning following code: http://jsfiddle.net/8PsdE/ <SELECT ID=pizzasize NAME=pizzasize> <OPTION VALUE=s>small <OPTION VALUE=m>medium
I am new to jQuery and need some help with the following problem. I
I need help converting the following code snippet to use SPSiteDataQuery instead of SPQuery
I have the following code. I need help fixing it such that the Category
I have the following code: var string = word1;word2;word3,word4,word5,word6.word7; function ends_with(string, character) { var
Need help on this following code. the problem is it only trigger the last
I need help with the following code that requires me to: Declare 3 double

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.