[英]Cast IEnumerable<Dictionary<string,object>> to a single merged Dictionary C#
First of all, I have a class that looks like this:首先,我有一个看起来像这样的类:
class Bag
{
public string Name { get; set; }
public int Number { get; set; }
}
Then I have the larger Dictionary that looks like this:然后我有一个更大的字典,看起来像这样:
Dictionary<string, List<Bag>> allBags = ParseLinesIntoBags(lines);
Now I want to get a Dictionary of the same type as the upper one, but with the condition that the List<Bag> must contain a specific name (in my case "shiny gold").现在我想得到一个与上一个相同类型的字典,但条件是 List<Bag> 必须包含一个特定的名称(在我的例子中是“闪亮的黄金”)。 For the further clarification if the allBags has the following:
为了进一步澄清 allBags 是否具有以下内容:
{ "black", new List<Bag>() {new Bag("red", 1), new Bag("blue", 2) } },
{ "yellow", new List<Bag>() {new Bag("shiny gold", 1), new Bag("blue", 2) } },
{ "pink", new List<Bag>() { new Bag("blue", 2), new Bag("shiny gold", 3), new Bag("green", 4) } }
the other Dictionary therefore should have:因此,另一本词典应该有:
{ "yellow", new List<Bag>() {new Bag("shiny gold", 1), new Bag("blue", 2) } },
{ "pink", new List<Bag>() { new Bag("blue", 2), new Bag("shiny gold", 3), new Bag("green", 4) } }
. . The task which I'm solving is from AdventOfCode2020 day 7 ( LINK ).
我正在解决的任务来自 AdventOfCode2020 第 7 天(链接)。
EDIT : ( since the original didn't follow the standards. I also deleted my failed tries because they were too far from the solution, and would only result in additional confusion .)编辑:(因为原始没有遵循标准。我也删除了我失败的尝试,因为它们离解决方案太远了,只会导致额外的混乱。)
So currently I have the following code (Special thanks to @AliReza for coming with an almost full solution):所以目前我有以下代码(特别感谢@AliReza 提供了一个几乎完整的解决方案):
using System.Collections.Generic;
using System.Linq;
namespace Day7
{
public class Bag
{
public string Name { get; set; }
public int Number { get; set; }
public Bag(string name, int number)
{
Name = name;
Number = number;
}
}
public class Program
{
public static void Main(string[] args)
{
Dictionary<string, List<Bag>> allBags = new Dictionary<string, List<Bag>>(){
{ "black", new List<Bag>() {new Bag("red", 1), new Bag("blue", 2) } },
{ "yellow", new List<Bag>() {new Bag("shiny gold", 1), new Bag("blue", 2) } },
{ "pink", new List<Bag>() { new Bag("blue", 2), new Bag("shiny gold", 3), new Bag("green", 4) } }};
string condition = "shiny gold";
var containerBagsV =from item in allBags
from bag in item.Value
where bag.Name == condition
select new Dictionary<string, List<Bag>>() { { item.Key, item.Value.ToList() } };
}
}
}
I wonder now is there any way to strongly type the Dictionary insted of leaving it to var?我想知道现在有什么方法可以强输入字典而不是将它留给 var 吗? If I change var to Dictionary<string, List> immidietly
如果我立即将 var 更改为 Dictionary<string, List>
Dictionary<string, List<Bag>> containerBagsV = from item in allBags
from bag in item.Value
where bag.Name == condition
select new Dictionary<string, List<Bag>>() { { item.Key, item.Value.ToList() } };
I get the following compile error:我收到以下编译错误:
Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<Day7.Bag>>>' to 'System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<Day7.Bag>>'.
严重性代码描述项目文件行抑制状态错误 CS0266 无法将类型 'System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<Day7.Bag>>>' 隐式转换为 ' System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<Day7.Bag>>'。 An explicit conversion exists (are you missing a cast?)
存在显式转换(您是否缺少演员表?)
If I try to cast it如果我尝试投射它
Dictionary<string, List<Bag>> containerBagsV = (Dictionary<string, List<Bag>>)(from item in allBags
from bag in item.Value
where bag.Name == condition
select new Dictionary<string, List<Bag>>() { { item.Key, item.Value.ToList() } });
I get the following exception: System.InvalidCastException: 'Unable to cast object of type我收到以下异常: System.InvalidCastException: 'Unable to cast object of type
'WhereSelectEnumerableIterator
2[<>f__AnonymousType0
2[System.Collections.Generic.KeyValuePair2[System.String,System.Collections.Generic.List
1[Day7.Bag]],Day7.Bag],System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List
1[Day7.Bag]]]' to type 'System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List
1[Day7.Bag]]'.''WhereSelectEnumerableIterator
2[<>f__AnonymousType0
2[System.Collections.Generic.KeyValuePair2[System.String,System.Collections.Generic.List
1[Day7.Bag]],Day7.Bag],System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List
1[Day7.Bag]]]'输入'System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List
1[Day7.Bag] ]'.'
I think you should get the idea with this example我想你应该明白这个例子
var allBags = new Dictionary<string, List<Bag>>
{
{ "black", new List<Bag>() { new Bag("red", 1), new Bag("blue", 2) } },
{ "yellow", new List<Bag>() { new Bag("shiny gold", 1), new Bag("blue", 2) } },
{ "pink", new List<Bag>() { new Bag("blue", 2), new Bag("shiny gold", 3), new Bag("green", 4) } }
};
var containerBag = from item in allBags
from bag in item.Value
where bag.Name == "shiny gold"
select item;
// this return yellow and pink records, you can select bag here also if you want
also if you want a dictionary output :如果你想要一个字典输出:
var containerBag = from item in allBags
from bag in item.Value
where bag.Name == "shiny gold"
select new Dictionary<string, List<Bag>>() { { item.Key, item.Value.ToList() } };
UPDATE merged version :更新合并版本:
var containerBag = from item in allBags
from bag in item.Value
where bag.Name == "shiny gold"
select item;
Dictionary<string, List<Bag>> mergedDictionary = new(containerBag);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.