c# - Where 条件中的字符串列表

标签 c# linq

下面是我的类(class):

public class Test
{
    public string Name {get;set;}
    public int  ID {get;set;}
    public IList<string> Tags {get;set;}

    Public Test()
    {
        Tags = new List<string>();
    }
}

现在我有另一个列表,名为 tags哪种数据类型是 List<string>具有一些值。

现在我想编写一个查询:

var results = List<Test>().Where(x=>x.Tags.Contains(tags));

但是tags禁止入内Contains ,自 Contains仅允许字符串类型。

实际错误是:

Argument 1: cannot convert from 'System.Collections.Generic.IList' to 'string'

我可以知道如何在 Lambda 中处理这种情况吗?

最佳答案

我假设您希望获得第二个列表 tags 中至少有一个标签的所有结果。您可以使用 Intersect 来执行此操作:

var results = List().Where(x=>x.Tags.Intersect(tags).Any())

关于c# - Where 条件中的字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67057206/

相关文章:

c# - 对 Linq 和分组感到困惑

c# - 非常简单的lambda表达式

c# - 推荐一个扎实的.Net FTP库

javascript - 如何在 bootstrap Modal asp.net mvc 中使用编辑

c# - 为什么没有 linq for byte, uint?

c# - LINQ to Entities 跳过并采取

c# - 更新 XML 文件 (C#/Linq)

c# - 如何将多个 bool 值声明为 false

c# - volatile 关键字是否确保线程的缓存更新?

c# - 使用闭包跟踪变量 : Good idea or dirty trick?