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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:41:14+00:00 2026-06-11T16:41:14+00:00

I need to select a number of values (into a List) from a Dictionary

  • 0

I need to select a number of values (into a List) from a Dictionary based on a subset of keys.

I’m trying to do this in a single line of code using Linq but what I have found so far seems quite long and clumsy. What would be the shortest (cleanest) way to do this?

This is what I have now (the keys are Strings and keysToSelect is a List of keys to select):

List<ValueType> selectedValues = dictionary1.Where(x => keysToSelect.Contains(x.Key))
                                            .ToDictionary<String, valueType>(x => x.Key,
                                                                             x => x.Value)
                                            .Values.ToList;

Thank you.

  • 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-11T16:41:15+00:00Added an answer on June 11, 2026 at 4:41 pm

    Well you could start from the list instead of the dictionary:

    var selectedValues = keysToSelect.Where(dictionary1.ContainsKey)
                         .Select(x => dictionary1[x])
                         .ToList();
    

    If all the keys are guaranteed to be in the dictionary you can leave out the first Where:

    var selectedValues = keysToSelect.Select(x => dictionary1[x]).ToList();
    

    Note this solution is faster than iterating the dictionary, especially if the list of keys to select is small compared to the size of the dictionary, because Dictionary.ContainsKey is much faster than List.Contains.

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

Sidebar

Related Questions

I'm trying to execute a query like this: SELECT * FROM table WHERE id
I need the number of line items per Order. In this example, order 10
Given a table action(start:DATE, length:NUMBER, type:NUMBER), with all records unique, I need to select
I need to select elements of a certain class except children of $(this). The
I need to select only 20 records from the table and than add pagination
I need to select all from table1 where active=1 and replace t1cid,t2cid,L1cid,L2cid---L10cid with corresponding
I am trying to import data from Access to Excel based on two parameters.
I have a number of enums and need to get them as List<string> objects
I'm trying to insert variables from an SQL result into a HTML form in
I need to select a top row for each category from a known set

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.