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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:37+00:00 2026-05-27T06:58:37+00:00

i created the following code: Dictionary<string, string> allItemNames = new Dictionary<string, string>(); var productNames

  • 0

i created the following code:

Dictionary<string, string> allItemNames = new Dictionary<string, string>();


var productNames = from product in entities.tbl_producttype
                               select new { ProductName = product.Name, ProductTitle = product.TitleName };

            foreach (var productName in productNames)
            {
                allItemNames.Add(productName.ProductName, productName.ProductTitle);
            }

it works great, but can i make the code shorter by dropping the ‘foreach’ phrase and make the query do the insert into the dictionary? like some kind of an ‘into’ phrase of the linq that tells the query to “insert the productName into the first string of the dictionary and the ProductTitle into the second”?

  • 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-27T06:58:37+00:00Added an answer on May 27, 2026 at 6:58 am

    It can confuse code to make modifications whilst querying. Your current implementation is easily readable and expresses the intent of the loop. As such there is no method on an Enumerable to do this.

    On a List you could use the ForEach method, however the cleanest way would be to use the LINQ ToDictionary method.

    var productNames = 
     from product in entities.tbl_producttype
      select new { ProductName = product.Name, ProductTitle = product.TitleName };
    var allItemNames = 
     productNames 
      .ToDictionary(product => product.ProductName, product => product.ProductTitle);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
I created a new project with the following code segment: char* strange = (Strange??);
I am using EF 4 and have the following code. public static Dictionary<Tuple<int, string>,
I have the following code in C# that returns a Dictionary<string, List<Discount>> . static
I created the following code to calculate the duration between two timestamps which can
I have a transparent BufferedImage created with the following code(not relevant how it is
I have created an alert view with two buttons using the following code: UIAlertView
I created a grid of buttons. The following code creates the buttons and displays
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
I created the following function to simplify a piece of particularly complex code. CREATE

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.