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 Query
- How can you convert a List<X> to Dictionary<X,Y> filling the value with null?
- How can you do custom sorting in LINQ with null always on the end?
- How can I substitute a value if a field is null with Linq?
- How can I concatenate the array content with its index value
- How can you use LINQ to find Azure AD users with specific licenses using the Azure AD Graph API Client Library 2.0
- Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?
- How can I get the value with most occurrences in a collection?
- How can you take only the last result with Linq
- How can I find the first items in a list with a different value for a property using Linq?
- How can I use LINQ to get the instance with the highest value of a property in a child list?
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- How can I get LINQ to return the object which has the max value for a given property?
- How can you handle an IN sub-query with LINQ to SQL?
- How to convert a List<T> into a comma-separated list, using the class's Id property as the value
- Convert an array to dictionary with value as index of the item and key as the item itself
- How can I find the selected RadioButton's value in ASP.NET?
- How to assign empty string if the value is null in linq query?
- How to Convert the value in DataTable into a string array in c#
- How can you update a Linq Expression with additional parameters?
- How can I get the first element after an element with LINQ-to-XML?
- How can you see the sql that is causing an error on SubmitChanges in LINQ to SQL?
- LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- How can I get LINQ to return the index of the object which has the max value in a collection?
- How can I convert a Predicate<T> to an Expression<Predicate<T>> to use with Moq?
- Can you advise me a resource with LINQ/lambda code exercises?
- How do you sort a String List by the number of words matched with an Array in Linq
- How can I make my Linq select return values if the value selected is null?
- How can I group by the difference between rows in a column with linq and c#?
- How can I return the Value from a .NET IGrouping via a Key?
- Linq query, how to check for a null value and use the value 0 in place of a null?
More Query from same tag
- LINQ vb.net GROUP BY - SELECT JOINED column
- Linearize with Linq in C#
- LINQ Distinct(), but programmatically select a winner
- Fill correctly a IQueryable model with linq join
- Passing different values to the SELECT NEW part of a LINQ query
- Linq Convert.ToInt32 in Query
- Eager loading related entities from a table-valued function in NHibernate
- Linq result to double(Multidimensional-arrays) array?
- LINQ query to Group and get Sum
- Create generic Function to filter many Objects that have the same Base Class
- SQL query to LINQ using group / sum / having
- C#/LINQ: Trying to optimize performance
- Linq Query with Null for Related Field
- Sorting Datagridview datasource on List<T> where T is anonymous
- EF Core Include method not working
- Query a part of XML and write to another variable along with common tag
- LINQ - BETWEEN "a" AND "e"
- set default value for nulls in result of a linq join
- Linq multiple levels in
- Why does the Linq Cast<> helper not work with the implicit cast operator?
- IQueryable - how to get all unique Years from db set?
- Get Earliest date from from IEnumerable<DateTime>
- "Left XOR" between two lists with LINQ
- How to get top 4 result from a list using linq c#
- Tracing LINQ TO SQL generated queries in ASP.NET MVC
- Get latest record from a grouping and filter on status
- Linq GroupJoin select query
- Linq: The "opposite" of Take?
- When using GroupBy in linq, how can I get a list of group names?
- Filtering nullable items in the List - LINQ