从字典列表中提取记录,其中键和值不包含给定值列表c# List firstList=newlist(); Dictionary dict=新字典(); 添加(“名称”、“abc”); 添加(“年龄”、“22”); 添加(“地址”,“xyz,aa”); 添加(“联系人”、“111”); firstList.Add(dict); Dictionary dict2=新字典(); 添加(“名称”、“pqr”); 第2条加入(“年龄”、“25”); 添加(“地址”,“xxx,bb”); 第2条添加(“联系人”、“4222”); 第一个列表。添加(第2条); Dictionary dict3=新字典(); 第3条添加(“名称”、“aa”); 第3条加入(“年龄”、“24”); 第3条添加(“地址”,“xxx,aa”); 第3条添加(“联系人”、“aaa”); 第一个列表。添加(第3条);

从字典列表中提取记录,其中键和值不包含给定值列表c# List firstList=newlist(); Dictionary dict=新字典(); 添加(“名称”、“abc”); 添加(“年龄”、“22”); 添加(“地址”,“xyz,aa”); 添加(“联系人”、“111”); firstList.Add(dict); Dictionary dict2=新字典(); 添加(“名称”、“pqr”); 第2条加入(“年龄”、“25”); 添加(“地址”,“xxx,bb”); 第2条添加(“联系人”、“4222”); 第一个列表。添加(第2条); Dictionary dict3=新字典(); 第3条添加(“名称”、“aa”); 第3条加入(“年龄”、“24”); 第3条添加(“地址”,“xxx,aa”); 第3条添加(“联系人”、“aaa”); 第一个列表。添加(第3条);,c#,asp.net-mvc,linq,dictionary,C#,Asp.net Mvc,Linq,Dictionary,返回列表中不包含key='address'和name='aa'的记录 更新:-返回记录,其中name='aa'使用linq非常简单: List<Dictionary<string, string>> firstList = new List<Dictionary<string, string>>(); Dictionary<string, string> dict = new Dictionary<string,string

返回列表中不包含key='address'和name='aa'的记录


更新:-返回记录,其中name='aa'

使用linq非常简单:

List<Dictionary<string, string>> firstList = new List<Dictionary<string, string>>();   
Dictionary<string, string> dict = new Dictionary<string,string>();
dict.Add("name", "abc");
dict.Add("age", "22");
dict.Add("address", "xyz,aa");
dict.Add("contact", "111");
firstList .Add(dict);
Dictionary<string, string> dict2 = new Dictionary<string,string>();
dict2 .Add("name", "pqr");
dict2 .Add("age", "25");
dict2 .Add("address", "xxx,bb");
dict2 .Add("contact", "4222");
firstList .Add(dict2);
Dictionary<string, string> dict3 = new Dictionary<string,string>();
dict3 .Add("name", "aa");
dict3 .Add("age", "24");
dict3 .Add("address", "xxx,aa");
dict3 .Add("contact", "aaa");
firstList .Add(dict3);
如果只需要一条记录,则使用
FirstOrDefault()

别忘了在顶部添加:

var result = firstList.Where(x => !(x.ContainsKey("address") 
                                && x.ContainsKey(name)
                                && x["name"] == "aa")).FirstOrDefault();
乌德帕特:
为什么你有(很多)具有相同键的字典,而不是一个具有(正确键入的)属性
Name
Age
int
)、
Address
Contact
)的专用类?如果需要通过
字符串
键访问它,则始终可以实现。但是一门专门的课程通常比一堆“严格打字”的字典引起的头痛要少得多然后它将变成
list.Where(item=>item.Address为null&&item.Name==“aa”)
@Dale我使用的是动态列表,所以我不能用属性指定专用类,我不知道如何创建通用专用类,哪些属性是动态的,所以我使用的是字典,实际上我需要那些键为“name”且值不等于“aa”的记录@shahbazusmani您需要相应地更新您的条件,查看更新的答案谢谢您更新的答案对我有用,还有一件事我想问您,我是否要提取名称不包含“a”的列表,将条件更改为:
!x[“名称”]。包含(“a”)
var result = firstList.Where(x => !(x.ContainsKey("address") 
                                && x.ContainsKey(name)
                                && x["name"] == "aa")).FirstOrDefault();
using System.Linq;
var result = firstList.Where(x => 
                                && x.ContainsKey(name)
                                && x["name"] != "aa")).FirstOrDefault();