首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从List <KeyValuePair <string,string >>返回匹配

从List <KeyValuePair<string,string >>返回匹配的项目,可以使用LINQ查询。以下是一个C#代码示例:

代码语言:csharp
复制
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("apple", "fruit"),
            new KeyValuePair<string, string>("banana", "fruit"),
            new KeyValuePair<string, string>("carrot", "vegetable"),
            new KeyValuePair<string, string>("orange", "fruit")
        };

        string searchKey = "fruit";
        var matchedItems = list.Where(item => item.Value == searchKey);

        foreach (var item in matchedItems)
        {
            Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
        }
    }
}

在这个示例中,我们创建了一个包含水果和蔬菜的List<KeyValuePair<string, string>>。然后,我们使用LINQ查询从列表中返回值为“fruit”的所有项目。最后,我们遍历并打印匹配的项目。

这个示例使用C#编写,但是你可以在其他编程语言中实现类似的功能。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券