Answered by:
How can i extract certain values from a dictionary ?

Question
-
User1253338400 posted
Hi
I have a dictionary with the following values:
Dictionary<string, string> myDict = new Dictionary<string, string>();
myDict.Add("FirstName", "Name");
myDict.Add("LasatName", "Name");
myDict.Add("MiddleName", "Name");
myDict.Add("Street", "Address");
myDict.Add("Suburb", "Address");
myDict.Add("City", "Address");How can i extract only the value of the Key such that the value is Name.
So for example I want to extract FirstName, LastName , MiddleName so that I have something like
string.format ( FIRST = "FirstName", MIDDLE="MiddleName",LAST="LastName", ??????)
How can i get those values , firstname, lastname and middle name via a linq expression
thanks
Monday, August 19, 2019 6:46 AM
Answers
-
User665608656 posted
Hi robby,
If you want to get the key in the dictionary, I suggest you should get the key based on the value.
Here is the code:
Dictionary<string, string> myDict = new Dictionary<string, string>(); myDict.Add("FirstName", "Name"); myDict.Add("LasatName", "Name"); myDict.Add("MiddleName", "Name"); myDict.Add("Street", "Address"); myDict.Add("Suburb", "Address"); myDict.Add("City", "Address"); var keys = myDict.Where(x => x.Value == "Name" || x.Value == "Address").Select(x => x.Key).ToList();
You can refer to this link: LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
Here is the result :
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 20, 2019 2:52 AM
All replies
-
User-1038772411 posted
Hello, robby32
yes you can. please follow below link for your answer.
https://stackoverflow.com/questions/7306767/linq-to-convert-a-string-to-a-dictionarystring-string
addons link :
https://stackoverflow.com/questions/4886530/get-value-from-key-using-linq
https://stackoverflow.com/questions/43806926/get-single-value-from-dictionary-by-key
Thanks.
Monday, August 19, 2019 10:03 AM -
User303363814 posted
The value of the FirstName element is
myDict["FirstName"]
If you are trying to produce a string with three parts of the name then it would be something like
var fullName = string.Format("{0} {1} {2}", myDict["FirstName"], myDict["MiddleName"], myDict["LasatName"]);
Since all three values are the same it is a little difficult to work out exactly what you are trying to achieve. If you could specify different values for each of the entries and then show us the result you are trying to achieve it will help. Showing 'code' that does not make any sense doesn't help to specify the result required.
Monday, August 19, 2019 10:53 PM -
User665608656 posted
Hi robby,
If you want to get the key in the dictionary, I suggest you should get the key based on the value.
Here is the code:
Dictionary<string, string> myDict = new Dictionary<string, string>(); myDict.Add("FirstName", "Name"); myDict.Add("LasatName", "Name"); myDict.Add("MiddleName", "Name"); myDict.Add("Street", "Address"); myDict.Add("Suburb", "Address"); myDict.Add("City", "Address"); var keys = myDict.Where(x => x.Value == "Name" || x.Value == "Address").Select(x => x.Key).ToList();
You can refer to this link: LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
Here is the result :
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 20, 2019 2:52 AM