Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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

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,我有一本字典,里面有一个列表作为值。我想从列表中选择属于特定键的特定元素。到目前为止,我试过: Dictionary<int, List<bool>> dic = new Dictionary<int, List<bool>>(); dic.Add(1, new List<bool> { true, true, false }); var works = dic.Where(x => x.Key == 1).SingleOrDef

我有一本字典,里面有一个
列表
作为值。我想从列表中选择属于特定键的特定元素。到目前为止,我试过:

Dictionary<int, List<bool>> dic = new Dictionary<int, List<bool>>();
dic.Add(1, new List<bool> { true, true, false });
var works = dic.Where(x => x.Key == 1).SingleOrDefault().Value.Where(x => x == true).ToList();
var doesNotWork = dic.Where(x => x.Key == 2).SingleOrDefault().Value.Where(x => x == true).ToList();
Dictionary dic=newdictionary();
添加(1,新列表{true,true,false});
var works=dic.Where(x=>x.Key==1.SingleOrDefault().Value.Where(x=>x==true.ToList();
var doesNotWork=dic.Where(x=>x.Key==2).SingleOrDefault().Value.Where(x=>x==true.ToList();
第一个LINQ有效,因为存在一个等于1的密钥。因此,我得到了一个包含两个元素的
列表。
第二个LINQ不工作,因为值为
null
。如何重写LINQ,以便在字典中没有合适的键时,得到一个空的
列表


我认为我的方法会起作用,因为我认为默认元素的值是空的,而不是空的。

最简单的解决方案是使用
Dictionary.TryGetValue
,这样您就不用检查两次值,即:

        Dictionary<int, List<bool>> dic = new Dictionary<int, List<bool>>();
        dic.Add(1, new List<bool> { true, true, false });

        List<bool> match = null;
        var found = dic.TryGetValue(2, out match);
        if (!found) match = new List<bool>();
Dictionary dic=newdictionary();
添加(1,新列表{true,true,false});
列表匹配=null;
找到的var=dic.TryGetValue(2,不匹配);
如果(!found)match=new List();

最简单的解决方案是使用
Dictionary.TryGetValue
,这样您就不会对某个值进行两次检查,即:

        Dictionary<int, List<bool>> dic = new Dictionary<int, List<bool>>();
        dic.Add(1, new List<bool> { true, true, false });

        List<bool> match = null;
        var found = dic.TryGetValue(2, out match);
        if (!found) match = new List<bool>();
Dictionary dic=newdictionary();
添加(1,新列表{true,true,false});
列表匹配=null;
找到的var=dic.TryGetValue(2,不匹配);
如果(!found)match=new List();

为什么它必须是LINQ

List<bool> works1 = dic.ContainsKey(1) ? dic[1] : new List<bool>();
List works1=dic.ContainsKey(1)?dic[1]:新列表();

为什么它必须是LINQ

List<bool> works1 = dic.ContainsKey(1) ? dic[1] : new List<bool>();
List works1=dic.ContainsKey(1)?dic[1]:新列表();
试试这个

Dictionary<int, List<bool>> dic = new Dictionary<int, List<bool>>();
dic.Add(1, new List<bool> { true, true, false });
var works = !dic.ContainsKey(1)? new List<bool>(): dic[1].Where(x => x == true).ToList();
var doesNotWork = !dic.ContainsKey(2) ? new List<bool>(): dic[2].Where(x => x == true).ToList();
Dictionary dic=newdictionary();
添加(1,新列表{true,true,false});
var工作=!集装箱司基dic(1)?新列表():dic[1]。其中(x=>x==true)。ToList();
var doesNotWork=!集装箱司基副总裁(2)?new List():dic[2]。其中(x=>x==true)。ToList();
试试这个

Dictionary<int, List<bool>> dic = new Dictionary<int, List<bool>>();
dic.Add(1, new List<bool> { true, true, false });
var works = !dic.ContainsKey(1)? new List<bool>(): dic[1].Where(x => x == true).ToList();
var doesNotWork = !dic.ContainsKey(2) ? new List<bool>(): dic[2].Where(x => x == true).ToList();
Dictionary dic=newdictionary();
添加(1,新列表{true,true,false});
var工作=!集装箱司基dic(1)?新列表():dic[1]。其中(x=>x==true)。ToList();
var doesNotWork=!集装箱司基副总裁(2)?new List():dic[2]。其中(x=>x==true)。ToList();

您不应该使用LINQ在
字典中查找密钥
-
字典
有更有效的方法来查找密钥-
包含密钥
/indexer对或更优化的
TryGetValue

例如:

int key = 2;
(A)

var result=dic.ContainsKey(键)?dic[key]。其中(x=>x==true)。ToList():新列表();
(B)

列表值;
var结果=dic.TryGetValue(输入、输出值)?其中(x=>x==true).ToList():new List();

您不应该使用LINQ在
字典中查找密钥
-
字典
有更有效的方法来查找密钥-
包含密钥
/indexer对或更优化的
TryGetValue

例如:

int key = 2;
(A)

var result=dic.ContainsKey(键)?dic[key]。其中(x=>x==true)。ToList():新列表();
(B)

列表值;
var结果=dic.TryGetValue(输入、输出值)?其中(x=>x==true).ToList():new List();

您希望得到一个奇怪的结果,但无论如何,此代码可以帮助您:

 var reslt = (dic.FirstOrDefault(x => x.Key == 2).Value ?? new List<bool>(0))
.Where(x => x)
.ToList();
var reslt=(dic.FirstOrDefault(x=>x.Key==2.Value??新列表(0))
.其中(x=>x)
.ToList();
使用方法SingleOrDefault()不正确,因为字典中的键是唯一的


x=>x==true也很奇怪。

您希望得到一个奇怪的结果,但无论如何,这段代码可以帮助您:

 var reslt = (dic.FirstOrDefault(x => x.Key == 2).Value ?? new List<bool>(0))
.Where(x => x)
.ToList();
var reslt=(dic.FirstOrDefault(x=>x.Key==2.Value??新列表(0))
.其中(x=>x)
.ToList();
使用方法SingleOrDefault()不正确,因为字典中的键是唯一的


x=>x==true也很奇怪。

我们不知道为什么它需要是LINQ,但这可能是一个选项:

var nowItWork = dic.Where(x => x.Key == 2).SelectMany(x => x.Value).Where(x => x).ToList();

我们不知道为什么需要LINQ,但这可能是一个选项:

var nowItWork = dic.Where(x => x.Key == 2).SelectMany(x => x.Value).Where(x => x).ToList();

免责声明:这仅适用于C#6.0及更高版本(VS 2015+)

如果您确实想使用linq在一行中执行此操作,则可以使用
?。
运算符(null条件运算符)并获得如下行:

var shouldWork=dic.Where(x=>x.Key==2)?.SingleOrDefault().Value.Where(x=>x==true.ToList()??新列表()

这将把shouldWork设置为linq查询的结果或空列表。您可以将
新列表()
替换为您想要的任何内容

有关C#6.0中的新功能的信息,请参见和,特别是github网站上的此示例:

整数长度=客户?长度??0; // 如果客户为空,则为0

以及如何工作的描述

空条件运算符显示短路行为, 当紧接其后的成员链访问时,元素 只有在原始 接收者不是空的

编辑:自?。如果检查null,则可以将上述linq查询简化为:

var shouldWork=dic[key]?。其中(x=>x==true)。ToList()??新列表()


其中key是保存密钥的某个变量。免责声明:这仅适用于C#6.0及更高版本(VS 2015+)

如果您确实想使用linq在一行中执行此操作,则可以使用
?。
运算符(null条件运算符)并获得如下行:

var shouldWork=dic.Where(x=>x.Key==2)?.SingleOrDefault().Value.Where(x=>x==true.ToList()??新列表()

这将把shouldWork设置为linq查询的结果或空列表。您可以将
新列表()
替换为您想要的任何内容

有关新功能的信息,请参见和