从词典列表中获取值 [英] Fetching values from list of Dictionaries

查看:94
本文介绍了从词典列表中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想从词典列表中获取值,以获得以字母a,b,c,d开头的值。



我想实现的功能将整理以字母a,b,c,d和e,f,g,h和开头的字典列表。 .....和y,z。

列表<字典<字符串,对象>> neighborhoodList =  new  List< Dictionary< string,object>>(); 
string locs_id = string .Empty;

while (dr.Read())
{
locs_id = dr [ locationid]。ToString();
neighborhood = new Dictionary< string,object>();
neighborhood.Add( LocationID,dr [ locationid]。ToString());
neighborhood.Add( LocationName,dr [ locationName]。ToString());
neighborhoodList.Add(邻里);
}



现在我想只获得以字母a,b,c,d开头的 neighborhoodList 中的记录。我想对所有部分进行相同的处理,例如e,f,g,h和y,z。我需要将排序记录保存在字典列表中,因为我需要传递给点液体的字典列表。这个实现我不能改变因为它会有很大的改变。





谢谢



Umesh Tayade

解决方案

你可以通过linq这样做..



var a = neighborhoodList.Select(x => x.Name.StartsWith(a));



欲了解更多detials,请参阅:



http://www.c-sharpcorner.com/UploadFile/0f68f2/using-lambda-expression-over-a-list-in-C-Sharp/ [ ^ ]

您好,



准备好词典列表后,试试这个。

字典<字符串,对象> temp =  new  Dictionary< string,object>(); 

foreach (字典<字符串,对象> dict neighborhoodList)
{
if (dict.Keys.First()。ToString()。StartsWith( a)|| dict.Keys.First()。ToString()。StartsWith( b)|| dict.Keys.First()。ToString()。StartsWith( c)|| dict.Keys.First()。ToString()。StartsWith( d))
{
temp.Add(dict.Keys.First()。ToString(),dict.Values.First()。ToString() );
}
}
var items = 来自 temp
orderby pair.Key 升序
选择对;
neighborhoodList = new List< Dictionary< string,object>>(); // null列表,以便插入新值
foreach (KeyValuePair< string,object> pair in items)
{
temp = new Dictionary< string,object>();
temp.Add(pair.Key,pair.Value);
neighborhoodList.Add(temp);
}
// 现在您的列表已排序,键值以a,b,c开头,d。



如果将值存储在简单字典对象中,而不是在字典列表中,则会更简单。因为你可以在简单的字典对象中存储多个值。



希望它可以帮到你。

谢谢。


Hi All,

I want to fetch the values from list of dictionaries in order to get the values that started with letter a,b,c,d.

I want to implement the functionality that will sort out the list of dictionary started with letter a,b,c,d and e,f,g,h and ...... and y,z.

List<Dictionary<string, object>> neighborhoodList = new List<Dictionary<string, object>>();
string locs_id = string.Empty;

while (dr.Read())
{
   locs_id = dr["locationid"].ToString();
   neighborhood = new Dictionary<string, object>();
   neighborhood.Add("LocationID",dr["locationid"].ToString());
   neighborhood.Add("LocationName", dr["locationName"].ToString()); 
   neighborhoodList.Add(neighborhood);
}


Now i want to get only those record from the neighborhoodList that starts with letter a,b,c,d. The same process i want to do for all section such as e,f,g,h and y,z. I need to keep the sorted record in the list of dictionary becuase that list of dictionary i need to pass to dot liquid. That implementation i cannot change because it will be a big change.


Thanks

Umesh Tayade

解决方案

You can do like this by linq..

var a = neighborhoodList.Select(x => x.Name.StartsWith("a"));

For more detials refer this:

http://www.c-sharpcorner.com/UploadFile/0f68f2/using-lambda-expression-over-a-list-in-C-Sharp/[^]


Hi,

After your dictionary list ready, try this.

Dictionary<string, object> temp = new Dictionary<string, object>();

foreach (Dictionary<string, object> dict in neighborhoodList)
{
    if (dict.Keys.First().ToString().StartsWith("a") || dict.Keys.First().ToString().StartsWith("b") || dict.Keys.First().ToString().StartsWith("c") || dict.Keys.First().ToString().StartsWith("d"))
    {
        temp.Add(dict.Keys.First().ToString(),dict.Values.First().ToString());
    }
}
var items = from pair in temp
            orderby pair.Key ascending
            select pair;
neighborhoodList = new List<Dictionary<string, object>>(); // null your list, so that new value can be inserted
foreach (KeyValuePair<string, object> pair in items)
{
    temp = new Dictionary<string, object>();
    temp.Add(pair.Key, pair.Value);
    neighborhoodList.Add(temp);
}
//now your list is sorted and key value start with a,b,c,d.


It would be more simple if you store value in simple dictionary object, not in list of dictionary. Because you can store multiple value in simple dictionary object.

Hope it helps you.
Thanks.


这篇关于从词典列表中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆