c#Linq获取实体查询中列表元素的ID
我可以在一个查询中找到一个实体并吸引列表中的ID吗 实体c#Linq获取实体查询中列表元素的ID,c#,linq-to-sql,C#,Linq To Sql,我可以在一个查询中找到一个实体并吸引列表中的ID吗 实体 public class Blog : IEntity { public int Id { get; set; } public virtual IList<Category> Categories { get; set; } = new List<Category>(); } public class Category : IEntity { public int Id { get; s
public class Blog : IEntity
{
public int Id { get; set; }
public virtual IList<Category> Categories { get; set; } = new List<Category>();
}
public class Category : IEntity
{
public int Id { get; set; }
public virtual IList<Blog> Blogs { get; set; } = new List<Blog>();
}
_blogService.Blogs.Where(b=>b.Id==blogId).Include(b=>b.Categories)应该可以做到这一点。无需再次调用类别。
有关快速加载的详细信息,请检查此假设
Queryable()
为Queryable
,您应该能够使用以下选项:
_blogService
.Queryable()
.Include(x => x.Categories)
.Where(x => x.Id == id)
.Select(x => x.Categories.Select(c => c.Id))
.FirstOrDefault();
代码并不完全准确。请不要把它当作真正的C代码。我需要你的DBContext对象。
_blogService
.Queryable()
.Include(x => x.Categories)
.Where(x => x.Id == id)
.Select(x => x.Categories.Select(c => c.Id))
.FirstOrDefault();