Tutorial :is there a way to get the Order number of a Dictionary Item?


Dictionary<string,string> items = new Dictionary<string,string>();    IEnumerable<string> textvalues = from c in......//using linQ to query    string s = textvalues[items["book"]];  

In this case the textvalues array will accept a integer value to return the string value.How can i get the item number , say "items" has 5 itemnames and "book" is at first position then i must get 0.So textvalues[items["book"]] would be translated as textvalues[item[0]]

Ok i was trying to use OpenXML 2.0 to read Excel.The point here is there is no way i could specify a field name and get the value. So i was trying to iterate the first row of a worksheet, add the values to a dictionary Dictionary fieldItems so that when i say fieldItems["Status"] it would retrieve me the cell value based on the column number , in my case the column header name.ok here's the code for it.

        Dictionary<string, int> headers = new Dictionary<string, int>();          IEnumerable<string> ColumnHeaders = from cell in  (from row in worksheet.Descendants<Row>()                                              where row.RowIndex == 1                                              select row).First().Descendants<Cell>()                                             where cell.CellValue != null                                             select                                               (cell.DataType != null                                                 && cell.DataType.HasValue                                                 && cell.DataType == CellValues.SharedString                                               ? sharedString.ChildElements[                                                 int.Parse(cell.CellValue.InnerText)].InnerText                                               : cell.CellValue.InnerText);                int i=0;          Parallel.ForEach(ColumnHeaders, x => { headers.Add(x,i++); });        order.Number = textValues[headers["Number"]];  


(Darn, I didn't misread it after all, and there's no edit history in the first five minutes.)

Your question is somewhat confusingly presented... but it seems like you're basically trying to find the "position" of an item within a dictionary. There's no such concept in Dictionary<TKey, TValue>. You should regard it as a set of mappings from keys to values.

Obviously when you iterate over the entries in that set of mappings they will come out in some order - but there's no guarantee that it will bear any relation to the order in which the entries were added. If you use SortedDictionary<,> or SortedList<,> (both of which are really still dictionaries), you can get the entries in sorted key order... but it's not clear whether that would be good enough for you.

It's also not clear what you're really trying to achieve - you call textvalues an array in the text, but declare it as an IEnumerable<string> - and it looks like you're then trying to use an indexer with a string parameter...

EDIT: Okay, now the question has been edited, you've got a Dictionary<string, int> rather than a Dictionary<string, string>... so the whole thing makes more sense. Now it's easy:

order.Number = textValues.ElementAt(headers["Number"]);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »