在 Linq 中为一对多关系编写 lambda 表达式时遇到问题

Having trouble to write lambda expression for One to Many Relationship in Linq

我正在尝试使用 Linq Lambda 表达式提取 Categories 列表以及特定 userId 的相应 Tickets

类别:

public class Category
    {
        public int Id { get; set; }
        public string CategoryName { get; set; }
        public ICollection<Ticket> Tickets { get; set; }
    }

门票:

public class Ticket
    {
        public int Id { get; set; } 
        public string Title { get; set; }

        public User User { get; set; }
        public Category Category { get; set; }
    }

用户:

public class User : IdentityUser
    {
        public ICollection<Ticket> Tickets { get; set; }
    }

这是我到目前为止尝试过的方法,但它只是 returns 类别,如果它有票的话。 我想要所有带有门票的类别(对于特定的用户 ID)

            var query = from cate in _context.Categories.Include(c => c.Tickets)
                        join tickets in _context.Tickets
                        on cate.Id equals tickets.Category.Id
                        where tickets.User.Id.Equals(id)
                        select (cate);
DatabaseContext.Categories
                         .Include(c => c.Tickets)
                         .Where(c => c.Tickets.Any(t => t.User.Id.Equals(id)));

这可能对您有所帮助,我建议您先了解更多有关 linq 运算符的知识。