score:16
Accepted answer
You can also use the Linq Enumerable.ToDictionary
, specifying the key and value as null:
var myList = new List<string> { "first", "second" };
Dictionary<string, string> dict = myList.ToDictionary(
item => item, // key
item => (string)null // value
);
Note that you need to cast "null" to your element type, otherwise C# can't infer the type of the second lambda expression.
score:1
Just using a loop is simple enough:
foreach (var item in myList)
myDictionary.Add(item, null);
This assumes, of course, that you have already created the Dictionary
.
Source: stackoverflow.com
Related Articles
- How can you convert a List<X> to Dictionary<X,Y> filling the value with null?
- LINQ - Convert List to Dictionary with Value as List
- Convert SQL to Linq left join with null
- Convert an array to dictionary with value as index of the item and key as the item itself
- Linq GroupBy with each null value as a group
- Linq GroupBy on multiple columns with potentials null value
- LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- SQL Trigger is trying to insert a null value but my C# code is passing int 300?
- Convert Dictionary<int, int?> to Dictionary<int, int> by skipping null values with LINQ
- Linq: Convert list of object to dictionary, with an object member as value
- How can I substitute a value if a field is null with Linq?
- "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."
- The null value cannot be assigned to a member with type System.Boolean which is a non-nullable value type
- Linq set null to a value with automapper
- The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- Checking for null value in c# var with LINQ & Entity framework
- Dealing with XElement null value
- Entity framework LINQ with value OR null
- The null value cannot be assigned to a member with type Int32 but strings work
- Return cell with value not null using linq - Epplus
- LINQ-to-SQL - The null value cannot be assigned to a member with type
- Get first value from table with where either have the value match or null with LINQ
- Need to convert a class into another class with distinct value collection
- Expression Lambda to order fields with null value
- LINQ & Entity Framework : issue with concatenating NULL value
- Can't add a new record with an integer value into database by using linq from code C#
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- Best way to add null value to IEnumerable as First Record with an example
- Exception: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- get selected value from combobox with data source is anonymous type
- VB - LINQ To XML: nested query with anon types from complex Xml file
- How to match specific properties within two lists in c# using LINQ
- How does OrderBy extension method orders collection
- Select only the lowest values with Linq
- Returning a large collection serialized as JSON via MVC controller
- Is there a better way of writing this Linq Query
- c# linq with selectMany to sql statement
- C#: Compare different sets of objects with the same property
- C# Prime Numbers with LINQ
- Differences between LINQ to Objects and LINQ to SQL queries
- Include Filters for Entity Framework Core
- Sql Linq search query
- How to structure a dynamic query with linq
- Parallel LINQ GroupBy taking long time on systems with high amount of cores
- Operator '&&' cannot be applied to operands of type 'bool' and 'System.Collections.Generic.IEnumerable<Feature>'
- LINQ over local variables
- Why can't I use the enumerator of an array, instead of implementing it myself?
- Problems with dynamic linq and nullable date is nulla
- How to TOP, ORDER BY and DISTINCT in the same query with Linq-To-Sql?
- split + substring + tolist c# Linq