Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
C#
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
Posted
Updated 17-Sep-13 21:09pm
v2

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/[^]
 
Share this answer
 
Hi,

After your dictionary list ready, try this.
C#
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.
 
Share this answer
 
Comments
Umesh Tayade 18-Sep-13 5:59am    
Hi Harshil,

Thank you very much for quick reply.
I have use code provided by you by making some changes in it and it works Smile | :) .
Thanks you again. you have provided me the exact solution that i was wanted.

Thanks

Umesh Tayade
Harshil_Raval 18-Sep-13 6:22am    
If you got your answer, then mark it as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900