Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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# C、 读取一个键值和另一个键值作为字典的值以列出_C# - Fatal编程技术网

C# C、 读取一个键值和另一个键值作为字典的值以列出

C# C、 读取一个键值和另一个键值作为字典的值以列出,c#,C#,我有一个字典,其中包含在etc上创建的类似资产id的信息,。我想提取id值作为键,创建值作为值,使用LINQ列出。下面是一个例子 Dictionary[0]= id:001, description:Test, name:SomeName, createdOn:11-09-2015 Dictionary[1]= id:002, description:Test2, name:SomeName2, createdOn:17-09-2015 我需要阅读列表作为 现在我尝试只读取一

我有一个字典,其中包含在etc上创建的类似资产id的信息,。我想提取id值作为键,创建值作为值,使用LINQ列出。下面是一个例子

Dictionary[0]=
    id:001, description:Test, name:SomeName, createdOn:11-09-2015

Dictionary[1]=
    id:002, description:Test2, name:SomeName2, createdOn:17-09-2015
我需要阅读列表作为

现在我尝试只读取一个值,如下所示

public Hits[] hits { get; set; };
hits.SelectMany(v => v.Source.Where(s => s.Key == "ObjId")).Select(s => s.Value).ToList()
课程在这里:

public class Hits
{     
    [DataMember(Name = "_source")]
    public Dictionary<string,string> Source { get; set; }
}

有人能给我建议实现这一目标的方法吗?

没有列表这样的东西。列表只有一种类型。如果需要多个类型的列表,请创建一个类来保存这些类型,或者使用元组

对于Linq,字典中已经有了项,所以在键等于的地方进行查找没有意义,只需从字典中读取值即可

下面应该生成一个包含id和createdOn字段的列表。这似乎是你想要的

hits.Select(x => new Tuple<string, string>(x.Source["id"], x.Source["createdOn"])).ToList();
或者你也可以

List<string> = hits.Select(x => string.Format("{0}:{1}", x.Source["id"], x.Source["createdOn"])).ToList();


非常不清楚。我想你想要一本字典,但你既没有显示你的类,也没有显示searchResult.srResponse.Hits或searchResult.srResponse.Hits.Hit是什么。@Tim,现在清楚了吗?你似乎想要一个列表,它。。。不存在。你可以有一个列表或一个字典仍然不清楚基本上我会有多个Hitspublic Hits[]Hits{get;set;},所以有多个字典。从这个列表中,我需要的值将来自不同键的值
List<string> = hits.Select(x => string.Format("{0}:{1}", x.Source["id"], x.Source["createdOn"])).ToList();
Dictionary<string, string> = hits.ToDictionary(x => x.Source["id"], x => x.Source["createdOn"]));