![]() |
| | #1 |
| Guest
Posts: n/a
| Using LINQ Dim RelatedKeys As String = (From kvp As KeyValuePair(Of String, String) In dicFieldsName Where kvp.Value = sel.Trim() Select kvp).First().Key > On Monday, September 22, 2008 4:42 AM ciupazNoSpamGrazi wrote: > Hi all, > having a dictionary of <int, string> (C# 2.0) is there a way to retrieve the > key for a specific value (of type string)? > Obviously I populate the Dictionary with not duplicated strings. > > Thanks a lot. > -- > Luigi >> On Monday, September 22, 2008 5:28 AM Mark Rae [MVP] wrote: >> http://www.google.co.uk/search?hl=en...by+value&meta= >> >> >> -- >> Mark Rae >> ASP.NET MVP >> http://www.markrae.net >>> On Monday, September 22, 2008 7:04 AM Jon Skeet [C# MVP] wrote: >>> Luigi <ciupazNoSpamGrazie@inwind.it> wrote: >>> >>> No - a dictionary is a one way mapping. If you want to be able to map >>> both ways, you need two dictionaries. >>> >>> -- >>> Jon Skeet - <skeet@pobox.com> >>> Web site: http://www.pobox.com/~skeet >>> Blog: http://www.msmvps.com/jon.skeet >>> C# in Depth: http://csharpindepth.com >>>> On Monday, September 22, 2008 9:24 AM OD wrote: >>>> dictionnaries are "one way". you'll have to iterate or use a second >>>> dictionnary. >>>> >>>> You said you're using C# 2.0, so just to show you what C#3.0 can bring, >>>> you can simplify your code using a Linq query like this (case >>>> insensitive search of string "two" in a dictionnary "dic" >>>> dictionnary<int,string>) : >>>> >>>> var key = (from k in dic where string.Compare(k.Value, "two", true) == >>>> 0 select k.Key).FirstOrDefault(); >>>> Console.WriteLine(key); >>>> >>>> -- >>>> >>>> >>>> OD___ >>>> www.e-naxos.com >>>>> On Tuesday, September 23, 2008 4:04 AM Marc Gravell wrote: >>>>> Probably the easiest option here is to loop over the pairs; >>>>> >>>>> public static int? FindKey( >>>>> this IDictionary<int, string> lookup, >>>>> string value) >>>>> { >>>>> foreach (var pair in lookup) >>>>> { >>>>> if (pair.Value == value) >>>>> return pair.Key; >>>>> } >>>>> return null; >>>>> } >>>>> >>>>> Which you can then call via: >>>>> >>>>> Dictionary<int, string> lookup = new Dictionary<int, >>>>> string>(); // etc >>>>> int? value = lookup.FindKey("abc"); >>>>> if (value.HasValue) >>>>> { >>>>> Console.WriteLine("Found it: " + value.Value); >>>>> } >>>>> >>>>> Marc >>>>>> On Friday, February 20, 2009 9:21 PM Tom Jackson wrote: >>>>>> Assuming you have a "deletedMsgs" dictionary and you want to remove a message from the "failedPosts" queue, but you need the failedPosts key that matches the value found in deletedMsgs.... >>>>>> >>>>>> >>>>>> >>>>>> int keyFound = 0; >>>>>> >>>>>> foreach (KeyValuePair<Guid, string> deleteEvent in deletedMsgs) >>>>>> >>>>>> { >>>>>> >>>>>> Console.WriteLine("Found deleted: " + deleteEvent.Value); >>>>>> >>>>>> // Check our dictionary to see if we're trying to repost >>>>>> >>>>>> // a message that was deleted in a later event >>>>>> >>>>>> if (failedPosts.ContainsValue(deleteEvent.Value)) >>>>>> >>>>>> { >>>>>> >>>>>> Console.WriteLine("Found " + deleteEvent.Value + " in failed posts and will be deleted."); >>>>>> >>>>>> // We found a failed post that was deleted >>>>>> >>>>>> // Get its key and remove it from the failedPosts dictionary >>>>>> >>>>>> foreach (KeyValuePair<int, string> failedKeyVal in failedPosts) >>>>>> >>>>>> { >>>>>> >>>>>> if (deleteEvent.Value == failedKeyVal.Value) >>>>>> >>>>>> { >>>>>> >>>>>> keyFound = failedKeyVal.Key; >>>>>> >>>>>> } >>>>>> >>>>>> } >>>>>> >>>>>> failedPosts.Remove(keyFound); >>>>>> >>>>>> Console.WriteLine("removed key: " + keyFound); >>>>>> >>>>>> } >>>>>> >>>>>> } >>>>>>> On Thursday, October 22, 2009 3:46 PM namdia wrote: >>>>>>> public int findMyValue(Dictionary<int,strnig> myDic, string val) >>>>>>> { >>>>>>> foreach(int key in myDic.Keys) >>>>>>> { >>>>>>> if ( myDic[key ] == val) >>>>>>> { >>>>>>> return key ; >>>>>>> } >>>>>>> } >>>>>>> return null; >>>>>>> } >>>>>>> >>>>>>> >>>>>>> >>>>>>> ciupazNoSpamGrazi wrote: >>>>>>> >>>>>>> Get Dictionary key by value >>>>>>> 22-Sep-08 >>>>>>> >>>>>>> Hi all, >>>>>>> having a dictionary of <int, string> (C# 2.0) is there a way to retrieve the >>>>>>> key for a specific value (of type string)? >>>>>>> Obviously I populate the Dictionary with not duplicated strings. >>>>>>> >>>>>>> Thanks >>>>>>> >>>>>>> Previous Posts In This Thread: >>>>>>> >>>>>>> ciupazNoSpamGrazi wrote: >>>>>>> >>>>>>> Get Dictionary key by value >>>>>>> Hi all, >>>>>>> having a dictionary of <int, string> (C# 2.0) is there a way to retrieve the >>>>>>> key for a specific value (of type string)? >>>>>>> Obviously I populate the Dictionary with not duplicated strings. >>>>>>> >>>>>>> Thanks >>>>>>> >>>>>>> Mark Rae [MVP] wrote: >>>>>>> >>>>>>> Re: Get Dictionary key by value >>>>>>> http://www.google.co.uk/search?hl=en...by+value&meta= >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Mark Rae >>>>>>> ASP.NET MVP >>>>>>> http://www.markrae.net >>>>>>> >>>>>>> Jon Skeet [C# MVP] wrote: >>>>>>> >>>>>>> Re: Get Dictionary key by value >>>>>>> No - a dictionary is a one way mapping. If you want to be able to map >>>>>>> both ways, you need two dictionaries. >>>>>>> >>>>>>> -- >>>>>>> Web site: http://www.pobox.com/~skeet >>>>>>> Blog: http://www.msmvps.com/jon.skeet >>>>>>> C# in Depth: >>>>>>> >>>>>>> OD wrote: >>>>>>> >>>>>>> dictionnaries are "one way". >>>>>>> dictionnaries are "one way". you will have to iterate or use a second >>>>>>> dictionnary. >>>>>>> >>>>>>> You said you are using C# 2.0, so just to show you what C#3.0 can bring, >>>>>>> you can simplify your code using a Linq que >>>>>>> >>>>>>> Marc Gravell wrote: >>>>>>> >>>>>>> Probably the easiest option here is to loop over the pairs; public >>>>>>> Probably the easiest option here is to loop over the pairs; >>>>>>> >>>>>>> public static int? FindKey( >>>>>>> this IDictionary<int, string> lookup, >>>>>>> string value) >>>>>>> { >>>>>>> foreach (var pair in lookup) >>>>>>> { >>>>>>> if (pair.Value == value) >>>>>>> r >>>>>>> >>>>>>> Tom Jackson wrote: >>>>>>> >>>>>>> Get dictionary key by value >>>>>>> Assuming you have a "deletedMsgs" dictionary and you want to remove a message from the "failedPosts" queue, but you need the failedPosts key that matches the value found in deletedMsgs.... >>>>>>> >>>>>>> int keyFou >>>>>>> >>>>>>> EggHeadCafe - Software Developer Portal of Choice >>>>>>> Programming C# >>>>>>> http://www.eggheadcafe.com/tutorials...ramming-c.aspx >>>>>>>> On Thursday, October 22, 2009 3:58 PM Peter Duniho wrote: >>>>>>>> namdia wrote: >>>>>>>> >>>>>>>> That does not compile. Even if we fix the spelling error, that is not a >>>>>>>> very efficient way to perform the search. Much better to just enumerate >>>>>>>> the KeyValuePair<TKey, TValue> instances in the Dictionary<TKey, >>>>>>>> TValue>, examining the Value property of each. That way, no additional >>>>>>>> overhead of looking up the value by key has to be incurred. >>>>>>>> >>>>>>>> And oddly enough, that more-efficient approach -- enumerating the pairs >>>>>>>> -- is in fact that Marc Gravell recommended to the original poster, OVER >>>>>>>> A YEAR AGO when the question was first asked. Why you are replying now, >>>>>>>> with a suggestion that is less efficient, I am a bit puzzled by. >>>>>>>> >>>>>>>> Pete >>>>>>>>> On Thursday, October 22, 2009 4:00 PM Scott M. wrote: >>>>>>>>> Aside from the fact that you cannot return null on a method that is not >>>>>>>>> marked as void, what is wrong with the code that you have? >>>>>>>>> >>>>>>>>> -Scott >>>>>>>>>> On Thursday, October 22, 2009 4:24 PM Jeff Johnson wrote: >>>>>>>>>> Wait, what? Did you mean "int" instead of "void"? >>>>>>>>>>> On Thursday, October 22, 2009 4:46 PM Peter Duniho wrote: >>>>>>>>>>> Jeff Johnson wrote: >>>>>>>>>>> >>>>>>>>>>> That would not have made sense either. If you want to return "null", you >>>>>>>>>>> have to be returning a nullable type. For example, "int?" >>>>>>>>>>> >>>>>>>>>>> When presented with a poor code example, it can be difficult to >>>>>>>>>>> correctly find and describe _every_ problem with it. On the bright>>>>>>>>>>> side, it is unlikely that exact code example will ever make it into a >>>>>>>>>>> real program, even with the errors corrected. >>>>>>>>>>> >>>>>>>>>>> Pete >>>>>>>>>>>> On Thursday, October 22, 2009 5:29 PM Jeff Johnson wrote: >>>>>>>>>>>> I mentally made a second correction (removal of "not") that I failed to type >>>>>>>>>>>> out. Here is what I was envisioning the statement should have been: "Aside >>>>>>>>>>>> from the fact that you cannot return null on a method that is marked as int." >>>>>>>>>>>>> On Thursday, October 22, 2009 6:56 PM Peter Duniho wrote: >>>>>>>>>>>>> Jeff Johnson wrote: >>>>>>>>>>>>> >>>>>>>>>>>>> Yes, that makes sense too. ![]() >>>>>>>>>>>>>> On Thursday, November 25, 2010 2:24 AM Ali Imam wrote: >>>>>>>>>>>>>> You can get key using LINQ >>>>>>>>>>>>>> >>>>>>>>>>>>>> like >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> Dim RelatedKeys As String = ( >>>>>>>>>>>>>> >>>>>>>>>>>>>> From kvp As KeyValuePair(Of String, String) >>>>>>>>>>>>>> >>>>>>>>>>>>>> In dicFieldsName Where kvp.Value = sel.Trim() Select kvp).First().Key >>>>>>>>>>>>>> Submitted via EggHeadCafe >>>>>>>>>>>>>> Merging SharePoint List Data into Word Documents >>>>>>>>>>>>>> http://www.eggheadcafe.com/tutorials...documents.aspx Advertisement |
|
![]() |
| Thread Tools | |
| Display Modes | |
| |
< Windows Help - MS Office Help >
| New To Site? | Need Help? |