简体   繁体   English

从字典 <int,List<Tuple<string,object> &gt;&gt;到字典 <int,List<Tuple<string,object,AnEnum> &gt;&gt;与LINQ

[英]From Dictionary<int,List<Tuple<string,object>>> to Dictionary<int,List<Tuple<string,object,AnEnum>>> with LINQ

Let's say I have a Dictionary<int,List<Tuple<string,object>>> d . 假设我有一个Dictionary<int,List<Tuple<string,object>>> d How could I create with LINQ, from d , a Dictionary<int,List<Tuple<string,object,AnEnum>>> d' (where AnEnum is an enum ) such that each AnEnum from Tuple<string,object,AnEnum> is determined by conditions on the <Tuple<string,object> ? 我如何用LINQ从d创建一个Dictionary<int,List<Tuple<string,object,AnEnum>>> d' (其中AnEnum是一个enum ),这样Tuple<string,object,AnEnum>中的每个AnEnum都是由<Tuple<string,object>上的条件确定?

Remark. 备注。 It's indeed quite a nested structure. 这确实是一个嵌套的结构。 Just to explain : in my world there are products that are caracterized by id's ( int 's), and that have fields (named with string 's) whose values are either numeric of string 's -- hence the object in the Tuple 's. 只是为了解释一下:在我的世界中,有一些商品用id( int )来表征,并且具有字段(用string '命名)的值是string 's的数字-因此Tupleobject ' s。 I have an api that to a list of id's List<int> and a list of fields List<string> associates a Dictionary<int,List<Tuple<string,object>>> . 我有一个API,它与ID的List<int>的列表和字段List<string>相关联,该Dictionary<int,List<Tuple<string,object>>>Dictionary<int,List<Tuple<string,object>>>关联起来。 According to the values of certains fields, I can know which "type" of product I am dealing with, type that I flag with an enum . 根据某些字段的值,我可以知道我要处理的产品的“类型”,即用enum标记的类型。

Umm maybe something like this: 嗯,也许是这样的:

d.ToDictionary(pair => pair.Key, 
               pair => pair.Value.Select(tuple => 
                              tuple.Create(tuple.Item1,
                                           tuple.Item2,                                                                                                     
                                           ConvertToEnum(tuple))).ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM