我正在尝试使用LINQ从字典中检索一些数据.
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");
Run Code Online (Sandbox Code Playgroud)
上面的行q1和q2都导致编译器错误.
error CS0742: A query body must end with a select clause or a group clause
Run Code Online (Sandbox Code Playgroud)
如何使用LINQ在字典中查找值?
谢谢,
干草堆
veg*_*rby 25
或
var q1 = from obj in testDict.Values where obj == "Apple" select obj;
Run Code Online (Sandbox Code Playgroud)
要么
var q1 = testDict.Where(p => p.Value == "Apple");
Run Code Online (Sandbox Code Playgroud)
你在你的陈述中有一个额外的"来自obj in",这是不需要的.删除它或将.Where更改为linq查询语法而不是方法语法.
var q1 = from obj in testDict.Values
where obj.Value == "Apple"
select obj;
var q2 = testDict
.Where(p => p.Value == "Apple");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35040 次 |
最近记录: |