Using Lambda with Dictionaries

c#, lambda, linq

Solution

Either

var q1 = from obj in testDict.Values where obj == "Apple" select obj;

or

var q1 = testDict.Where(p => p.Value == "Apple");

Problem

I am trying to use LINQ to retrieve some data from a dictionary. ``` var testDict = new Dictionary<int, string>(); testDict.Add(1, "Apple"); testDict.Add(2, "Cherry"); var q1 = from obj in testDict.Values.Where(p => p == "Apple"); var q2 = from obj in testDict.Where(p => p.Value == "Apple"); ``` The above lines, q1 and q2, both result in a compiler error. ``` error CS0742: A query body must end with a select clause or a group clause ``` How do I go about using LINQ to find values in a dictionary? Thank you, Rick

Original source