c# - 可枚举包含可枚举

标签 c# linq

对于方法,我具有以下参数IEnumerable<string> tags并用于查询对象列表,我们将它们称为Post,其中包含属性IEnumerable<string> Tags { get; set; }

我的问题是:
我如何使用linq从标签参数中查询包含所有标签的对象?

private List<Post> posts = new List<Post>();

public IEnumerable<Post> GetPostsWithTags(IEnumerable<string> tags)
{
  return ???;
}

最佳答案

public IEnumerable<Post> GetPostsWithTags(IEnumerable<string> tags)
{
  return posts.Where(post => tags.All(tag => post.Tags.Contains(tag)));
}

关于c# - 可枚举包含可枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2988106/

相关文章:

c# - 在安装时更改 app.config

c# - Entity Framework 平均值,按查询分组

c# - LINQ 实际编译成什么?

c# - Saxon XSLT 传递参数

c# - 使用 int[][] 和 int[,] 有什么区别?

使用正确凭据的 C# WebClient HTTP 基本身份验证失败 401

c# - Dapper SqlException 和 Unobserved 异常

c# - 在 LINQ 中使用 IN 运算符

c# - 根据文件夹结构对 url 字符串列表进行分组

c# - LINQ 查询 SQLite 时出错