score:78
you don't need to use linq to get the values. the dictionary(tkey, tvalue)
has a property that holds the values, dictionary(tkey, tvalue).values
:
var fields = objdictionary.values.tolist();
score:1
with c# 7 it is possible to use tuples, which makes this conversion much simpler and you can have the both the key and the value in the list.
var mylist = new list<(tkey, tvalue)>(dic.select(x => (tkey, tvalue)));
full example
dictionary<int, string> dic = new dictionary<int, string>();
dic.add(1, "test1");
dic.add(2, "test2");
dic.add(3, "test3");
var mylist = new list<(int, string)>(dic.select(x => (x.key, x.value)));
foreach (var myitem in mylist)
{
console.writeline($"int val = {myitem.item1} string = {myitem.item2}");
}
result
int val = 1 string = test1
int val = 2 string = test2
int val = 3 string = test3
score:5
you will get a compiler error simply trying to convert a dictionary's values to a list with tolist():
dictionary<int, int> dict = new dictionary<int, int>();
var result = dict.values.tolist();
unless you include "using system.linq" in your file.
Source: stackoverflow.com
Related Query
- Convert dictionary values to list using linq
- Convert a list to a dictionary and sum up values using linq
- Convert list to dictionary using linq and not worrying about duplicates
- How to get a list of filtered values in Dictionary into a List using LINQ or Lambda?
- Convert string[] to int[] in one line of code using LINQ
- LINQ - Convert List to Dictionary with Value as List
- How to select values within a provided index range from a List using LINQ
- How to select multiple values from a Dictionary using Linq as simple as possible
- Get indexes of all matching values from list using Linq
- How do I convert from a Dictionary to a SortedDictionary using LINQ in C#?
- Using LINQ to convert a list to a CSV string
- Select distinct values from a list using LINQ in C#
- Convert DataRow to Dictionary using LINQ
- C# LINQ - convert nested dictionary to a list
- Count values in Dictionary using LINQ and LINQ extensions
- Find the Second Max in a list of values using linq c#
- Group by key and send values into list using LINQ
- Convert string array to custom object list using linq
- convert comma separated string to list using linq
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- Assign values from one list to another using LINQ
- How to convert list of objects with two fields to array with one of them using LINQ
- List of Dictionary to List<Type> using Linq
- How to convert an array to a dictionary using LINQ
- How to Convert Anonymous Type to List <dynamic> using Linq to SQL in C#
- How to convert Dictionary to List with mixing of values and keys?
- Transfor List to Nested Dictionary using linq C#
- How to add list of missing values in c# list using Linq
- Cast troubles while using LINQ to filter a Dictionary by values
- C# - Convert delimited string array w/ duplicates to dictionary using LINQ
More Query from same tag
- How I can apply PagedList to a viewModel class instead of a Model class
- Linq-to-sql user generated predicate
- Convert this double match to LINQ
- Using XDocument how do I add a serialized/xml representation of a class as an element of XDocument?
- How to make short lambda of multi join?
- Linq guru - filtering related entities
- Error when aggregating in linq query
- find all id where deleted column value should be 0
- LINQ to Entities does not recognize the method 'Int16 Parse(System.String)'
- VS2015 does not see Linq in RazorEngine template
- How can I query TEntity based on property?
- Moq.CastleProxyFactory' Could not load file or assembly 'Castle.Core
- DataContext accessed after Dispose in render pages/controls
- Remove rows where column contains specific text
- EF core 3 nested group by after restricting client evaluation
- LINQ to SQL Grouping and Processing Slow
- Many to Many relationship using SQL and Linq (entity framework/to entities)
- Manipulating variable type within LINQ expression
- Multiple Where vs Inner Join
- Comparing collections and counting duplicates using LINQ and C#
- How do I append LINQ queries to each other?
- Convert T-SQL to Fluent Linq C# with the same SQL generated output
- Linq query using ID returns result to slow (EF Core)
- LINQ to XML - how do I obtain index
- Constructing a Sorted List with priority items
- Linq marking duplicates in a list base on multiple fields
- LINQ projection that pairs items and fills with null when nth item doesn't exist
- How to join an entity object with select query in LINQ c#
- Dynamically build an Expression tree for sorting
- Join between two repositories in Entity Framework Core