Sorting , searching a dictionary in C#
I have a dictionary, Dictionary listOfProducts. This listOfProducts can have hundreds of products. string key is the product number I want to get some specific products by product number(key), I have a list of keys e.g. 64290,64287,59261,50990,50975,50897,68494,68495,51015,68493 the resulting dictionary should have only items with the above keys and in the same order, Any idea how can i do this. ? If there is any order data structure that can work better in this scenario?
Here is the complete scenario :
Here is the complete sc开发者_运维技巧enario : I get an xml from server with the product info the formate of this XMl is
<key>64285</key><string>productname[SEPERATOR]product description</string> <key>64285</key><string>productname[SEPERATOR]product description</string><key>64285</key><string>productname[SEPERATOR]product description</string>
Now this XML does not contain all the prodcut detaqils (e.g. this product does not have the Product Rating , or Product Color) I need to sort and search by Product rating and Product Color. For that i get another XMl wich has the product keys order by Rating .
<dict> <key>rating</key> <string>64285,68495,68494,64228,68491.......</string> <key>price</key> <string>68493,64234,50863,64236,64223,....</string> </dict>
Similary i have to Search for the Product with the Color "White" For this i get XMl with the list the products that are in white color,
A Dictionary does not have an order. Maybe you can make a List which contains the elements in order.
List<Product> result = new List<Product>();
var keys = new [] {64290,64287,59261,50990,50975,50897,68494,68495,51015,68493};
foreach (var key in keys)
{
result.Add(listOfProducts[key]);
}
return result;
You can use a separate Array/List var keyList = new List<int>();
to store the keys in the order you want, that way you can iterate through the keyList
and do a lookup for the value in the original Dictionary.
Have you looked at the SortedDictionary?
Alternativly, I don't think you can rely on the order of items in the dictionary, but you could implement IComparable or IComparer interfaces in your object, then use those to sort and search.
or try a Queue?
Can you add more detail to the context of your question, I would make it easier if we knew why you were trying to achieve what you are asking for.
I believe a Lookup is probably the collection you're looking for, since you're mapping one key to many possible values (if I read correctly).
You can sort the entries however you want when creating the lookup, or when you're retrieving the values out of it.
Edit:
Here is an example:
var Products = new []
{
new { Key = "64290", Name = "P1" },
new { Key = "64290", Name = "P2" },
new { Key = "64287", Name = "P3" },
new { Key = "59261", Name = "P4" },
new { Key = "59261", Name = "P5" },
new { Key = "64290", Name = "P6" },
new { Key =" 64290", Name = "P7" },
new { Key = "51015", Name = "P8" },
new { Key = "51015", Name = "P9" }
};
Lookup<string,string> products = ( Lookup<string,string> ) Products.ToLookup
(
p => p.Key, p => p.Name
);
foreach( IGrouping<string,string> g in products )
{
Console.WriteLine( g.Key );
foreach( string product in g )
{
Console.WriteLine( String.Concat( "\t", product ) );
}
}
Output:
64290 P1 P2 P6 P7 64287 P3 59261 P4 P5 51015 P8 P9
精彩评论