在 SQL 上,我有 3 个表:Organization(id, name)、Category(id, name)、Catalog(org_id, cat_id) - 组织和类别之间的映射表。ADO.NET DbContext Generator 为我创建了 2 个类:
public partial class Organization
{
public Organization()
{
this.Category = new HashSet<Category>();
}
public int id { get; set; }
public string name { get; set; }
public virtual ICollection<Category> Category { get; set; }
}
public partial class Category
{
public Category()
{
this.Organization = new HashSet<Organization>();
}
public int id { get; set; }
public string name { get; set; }
public virtual ICollection<Organization> Organization { get; set; }
}
如何使用 LINQ 选择映射到 cat_id = 1 的所有组织?就像我使用 t-sql 一样:
SELECT *
FROM Organization o
INNER JOIN Catalog ct ON o.id = ct.org_id
INNER JOIN Category cg ON ct.cat_id = cg.id
WHERE cg.id = 1
我试过
var model = _db.Category
.Where(c => c.id == 1)
.Select(c => c.Organization);
但是我在视图中定义的类型有问题,比如
@model IEnumerable<Project1.Models.Organization>