Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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,我有一本这样的字典: IDictionary<foo, IEnumerable<bar>> my_dictionary 如何仅使用IsValid=true的项创建另一个字典 我试过这个: my_dictionary.ToDictionary( p=> p.Key, p=> p.Value.Where (x => x.IsValid)); 上面代码的问题是,如果该键的所有元素都是IsValid=f

我有一本这样的字典:

IDictionary<foo, IEnumerable<bar>> my_dictionary
如何仅使用IsValid=true的项创建另一个字典

我试过这个:

my_dictionary.ToDictionary( p=> p.Key,
                            p=> p.Value.Where (x => x.IsValid));
上面代码的问题是,如果该键的所有元素都是IsValid=false,那么这将创建一个可枚举为空的键

例如:

my_dictionar[foo1] = new List<bar> { new bar {IsValid = false}, new bar {IsValid = false}, new bar {IsValid = false}};
my_dictionary[foo2] = new List<bar> {new bar {IsValid = true} , new bar{IsValid = false};
var new_dict = my_dictionary.ToDictionary( p=> p.Key,
                            p=> p.Value.Where (x => x.IsValid));
// Expected new_dict should contain only foo2 with a list of 1 bar item.
// actual is a new_dict with foo1 with 0 items, and foo2 with 1 item.
myu字典[foo1]=newlist{newbar{IsValid=false},newbar{IsValid=false},newbar{IsValid=false};
my_dictionary[foo2]=new List{new bar{IsValid=true},new bar{IsValid=false};
var new\u dict=my\u dictionary.ToDictionary(p=>p.Key,
p=>p.Value.Where(x=>x.IsValid));
//预期的新目录应仅包含具有1条项目列表的foo2。
//actual是一个新的目录,其中foo1包含0项,foo2包含1项。
我怎样才能得到我的期望值。

像这样的东西

my_dictionary
    .Where(p=> p.Value.Any(x => x.IsValid))
    .ToDictionary( p=> p.Key,
                   p=> p.Value.Where (x => x.IsValid));
这将只包括至少一个
值有效的项目

my_dictionary.Where(p => p.Any(v => v.Value.IsValid())
             .ToDictionary(p=> p.Key,
                           p=> p.Value.Where(x => x.Value.IsValid());
仅获取值中为true的项,然后仅将值中为true的项获取到新命令中

过滤器然后创建dictional

var new\u dict=my\u dictionary。选择(x=>newkeyvaluepair(
var new_dict = my_dictionary.Select(x => new KeyValuePair<foo, List<bar>>(
                                                 x.Key,
                                                 x.Value
                                                  .Where(y => y.IsValid)
                                                  .ToList()))
                            .Where(x => x.Value.Count > 0)
                            .ToDictionary(x => x.Key, x => x.Value.AsReadOnly());
x、 钥匙, x、 价值观 .其中(y=>y.IsValid) .ToList()) .其中(x=>x.Value.Count>0) .ToDictionary(x=>x.Key,x=>x.Value.AsReadOnly());
+1 OP需要在调用
ToDictionary
之前使用
Where
过滤集合这第一个Where子句是否不足以去掉x=>!x.IsValid?您不能只写.ToDictionary(p=>p.Key,p=>p.Value),因为调用ToDictionary之前的where子句已经在应用筛选了?@Prokurors:没有,因为我们正在处理一个
IEnumerable
,所以它比这个稍微复杂一点。第一个.where()正在检查每个KeyValuePair中的任何
是否有效,但实际上并没有过滤
本身以仅包含有效的条。如果您有一个
KeyValuePair
的值包含有效和无效的混合,则以后仍需要过滤掉无效的条。
var new_dict = my_dictionary.Select(x => new KeyValuePair<foo, List<bar>>(
                                                 x.Key,
                                                 x.Value
                                                  .Where(y => y.IsValid)
                                                  .ToList()))
                            .Where(x => x.Value.Count > 0)
                            .ToDictionary(x => x.Key, x => x.Value.AsReadOnly());