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
A lambda is simply a way to define a function in Python. They are sometimes known as lambda operators or lambda functions. By now you probably have defined your functions using the def keyword, and it has worked well for you so far.
According to the python doc, you can indeed use the == operator on dictionaries.
Either
var q1 = from obj in testDict.Values where obj == "Apple" select obj;
or
var q1 = testDict.Where(p => p.Value == "Apple");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With