我有這樣的方法:動態包含在查詢預先加載報表 - EF 4.3.1
public CampaignCreative GetCampaignCreativeById(int id)
{
using (var db = GetContext())
{
return db.CampaignCreatives
.Include("Placement")
.Include("CreativeType")
.Include("Campaign")
.Include("Campaign.Handshake")
.Include("Campaign.Handshake.Agency")
.Include("Campaign.Product")
.AsNoTracking()
.Where(x => x.Id.Equals(id)).FirstOrDefault();
}
}
我想就包括動態列表。我試過了:
public CampaignCreative GetCampaignCreativeById(int id, string[] includes)
{
using (var db = GetContext())
{
var query = db.CampaignCreatives;
foreach (string include in includes)
{
query = query.Include(include);
}
return query.AsNoTracking()
.Where(x => x.Id.Equals(id)).FirstOrDefault();
}
}
但是沒有編譯。我得到這個錯誤:
不能隱式地將類型'System.Data.Entity.Infrastructure.DbQuery'轉換爲'System.Data.Entity.DbSet'。存在明確的轉換(您是否缺少演員?)
有誰知道如何製作包含動態列表?
感謝
我做了一個插件,做到這一點,這裏是鏈接https://www.codeproject.com/Tips/1205294/Entity-Framework-Dynamic-Include-Hier archy – 2017-09-11 16:48:14