gpt4 book ai didi

c# - 将 Lambda 与字典一起使用

转载 作者:太空狗 更新时间:2023-10-29 18:01:49 26 4
gpt4 key购买 nike

我正在尝试使用 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");

上面的行 q1 和 q2 都会导致编译器错误。

error CS0742: A query body must end with a select clause or a group clause

如何使用 LINQ 在字典中查找值?

谢谢,

瑞克

最佳答案

要么

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

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

关于c# - 将 Lambda 与字典一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1070158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com