This is my code
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}]
In the code i have a list of dictionaries with id and name keys. now i have id=102 search in list of dictionaries and get name value related on id using linq query.
var name = data1.First(d => d["id"] == "102")["name"];
您找到键“ id”映射到值“ 102”的第一个列表元素,然后获取键“名称”的值。
Try this
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.