MVC 多对多仅在代码中首先获取具有特定类型的实体
MVC many to many get entities with of certain type only in code first
我在 Company 和 CompanyType 之间存在多对多关系
我想获得一个下拉列表,其中包含 CompanyType.Name 为 "Client" 的公司。 IE。客户公司列表。
我如何在 linq 中做到这一点?
我基本上想做以下但在 linq
var clientCompanies = db.Companies.SqlQuery(@"SELECT c.*
FROM Companies AS c
JOIN CompanyTypeCompanies AS ctc ON ctc.Company_ID = c.ID
JOIN CompanyTypes AS ct ON ctc.CompanyType_ID = ct.ID
WHERE ct.Name = 'Client'").ToList();
您可以使用 include 语句,如果它在表之间具有适当的外键关系
var clientCompanies = db.CompanyTypes
.Where(ct => ct.Name == "Client").Include(ct => ct.CompanyTypeCompanies).Include(ct => ct.Companies).Select(ct => ct.Companies).ToList();
正在启动class
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}
我在 Company 和 CompanyType 之间存在多对多关系
我想获得一个下拉列表,其中包含 CompanyType.Name 为 "Client" 的公司。 IE。客户公司列表。
我如何在 linq 中做到这一点?
我基本上想做以下但在 linq
var clientCompanies = db.Companies.SqlQuery(@"SELECT c.*
FROM Companies AS c
JOIN CompanyTypeCompanies AS ctc ON ctc.Company_ID = c.ID
JOIN CompanyTypes AS ct ON ctc.CompanyType_ID = ct.ID
WHERE ct.Name = 'Client'").ToList();
您可以使用 include 语句,如果它在表之间具有适当的外键关系
var clientCompanies = db.CompanyTypes
.Where(ct => ct.Name == "Client").Include(ct => ct.CompanyTypeCompanies).Include(ct => ct.Companies).Select(ct => ct.Companies).ToList();
正在启动class
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}