这是我的代码
public class model
{
public model();
public List<Dictionary<string, string>> Data { get; set; }
}
List<Dictionary<string,string>> data1;
var data1 = await get<model>();
data1[0]=[0][{id,101}]
[1][{name,one}]
data1[1]=[0][{id,102}]
[1][{name,two}]
data1[2]=[0][{id,103}]
[1][{name,three}]
在代码中,我有一个带有id和name键的字典列表。现在我在字典列表中搜索id = 102,并使用linq查询获取与id相关的名称值。
答案 0 :(得分:1)
var name = data1.First(d => d["id"] == "102")["name"];
你找到第一个列表元素,其中键是#34; id&#34;映射到值&#34; 102&#34;,然后获取key&#34; name&#34;的值。
答案 1 :(得分:0)
试试这个
List<Dictionary<string, string>> data1 = new List<Dictionary<string, string>>();
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic1.Add("101", "one");
dic1.Add("102", "two");
data1.Add(dic1);
dic1 = new Dictionary<string, string>();
dic1.Add("201", "2one");
dic1.Add("202", "2two");
data1.Add(dic1);
dic1 = new Dictionary<string, string>();
dic1.Add("301", "3one");
dic1.Add("302", "3two");
data1.Add(dic1);
//try your values here
var id = "201";
var s=data1.Where(c => c.Keys.Contains(id)).Select(c => c.Keys.Where(p => p == id).Select(p => c[p]).FirstOrDefault()).FirstOrDefault();
Console.WriteLine(s);