-1

Is it possible to use LINQ to get data from DB and store it in Dictionary. I can do LINQ and store it in a List<Class_B> and then iterate through the list and store it in Dictonary<Class_A,List<Class_B>>. But is it possible to directly store in the Dictionary?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3344591
  • 567
  • 5
  • 21

1 Answers1

0

System.Linq.Enumerable has a ToDictionary method.

Here's an example from dotnetperls.com:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // Example integer array.
        int[] values = new int[] { 1, 3, 5, 7 };

        // First argument is the key, second the value.
        Dictionary<int, bool> dictionary = values.ToDictionary(v => v, v => true);

        // Display all keys and values.
        foreach (KeyValuePair<int, bool> pair in dictionary)
        {
            Console.WriteLine(pair);
        }
    }
}
Andy West
  • 12,302
  • 4
  • 34
  • 52