问题描述
我正在尝试使用Linq Lambda表达式为特定的userId提取 Categories 的列表以及相应的 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; }
}
这是我到目前为止尝试过的方法,但是如果有票证,它只会返回Category。 我想要所有带有票证的类别(用于特定的userId)
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运算符的更多信息。