C# 使用LINQ存储在哈希表中
是否可以使用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并将其存储在
列表中
,然后遍历该列表并将其存储在目录中
。但是可以直接存储在字典中吗 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);
}
}
}