IEnumerable to Dictionary .NET 3.5
suggest changeCreate a Dictionary<TKey, TValue> from an IEnumerable<T>:
using System; using System.Collections.Generic; using System.Linq;
public class Fruits { public int Id { get; set; } public string Name { get; set; } }
var fruits = new[] { new Fruits { Id = 8 , Name = "Apple" }, new Fruits { Id = 3 , Name = "Banana" }, new Fruits { Id = 7 , Name = "Mango" }, }; // Dictionary<int, string> key value var dictionary = fruits.ToDictionary(x => x.Id, x => x.Name);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents