Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ存储在哈希表中_C#_Linq - Fatal编程技术网

C# 使用LINQ存储在哈希表中

C# 使用LINQ存储在哈希表中,c#,linq,C#,Linq,是否可以使用LINQ从数据库中获取数据并将其存储在字典中。 我可以执行LINQ并将其存储在列表中,然后遍历该列表并将其存储在目录中。但是可以直接存储在字典中吗 System.Linq.Enumerable有一个方法 下面是一个例子: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 班级计划 { 静态void Main() { //整数数组示例。 int[]值=新的int[]{1,3,5,7}; //第一个参数是键,第二个参数是值。 Dicti

是否可以使用LINQ从数据库中获取数据并将其存储在字典中。
我可以执行LINQ并将其存储在
列表中
,然后遍历该列表并将其存储在
目录中
。但是可以直接存储在字典中吗

System.Linq.Enumerable有一个方法

下面是一个例子:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
班级计划
{
静态void Main()
{
//整数数组示例。
int[]值=新的int[]{1,3,5,7};
//第一个参数是键,第二个参数是值。
Dictionary Dictionary=values.ToDictionary(v=>v,v=>true);
//显示所有键和值。
foreach(字典中的KeyValuePair对)
{
控制台写入线(对);
}
}
}

是,使用
ToDictionary()
方法。看一看
todictionary
我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。
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);
        }
    }
}