【发布时间】:2020-03-02 06:49:21
【问题描述】:
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);
返回列表不包含 key = 'address' 和 name= 'aa' 的记录
更新:- 返回记录,其中 name='aa'
【问题讨论】:
-
为什么你有(很多)具有相同键的字典,而不是具有(正确键入)属性
Name、Age(int)、Address和 @ 的专用类987654327@?如果您需要通过string键访问它,您始终可以实现Indexer。但是一个专门的类通常比一堆“字符串类型”的字典引起的头痛要少得多。 -- 然后会变成list.Where(item => item.Address is null && item.Name == "aa"); -
@Dale 我正在使用动态列表,所以我无法指定具有属性的专用类,并且我不知道如何创建属性是动态的通用专用类,所以我使用字典
标签: c# asp.net-mvc linq dictionary