I have two dictionaries dict1 and dict2, I would like to remove items from dict1 which are present in dict2 using its key. Instead of looping through dict2 and using “ContainsKey” method, ia there any other approach like using linq.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The appropriate way to do this would be:
LINQ stands for Language Integrated Query. It is for performing queries on data. Queries do not mutate the underlying structures. Since what you want to do is to mutate one of the queries LINQ is not an appropriate tool.
Also note that
Removereturns a boolean indicating whether or not it actually removed the key. It doesn’t throw an exception. You don’t need to callContainsKeybefore calling remove (this will save you an extra table lookup for each item).