我有一个Company类:
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public int? AnalystId { get; set; }
public int? ReviewerId { get; set; }
public virtual Staff Analyst { get; set; }
public virtual Staff Reviewer { get; set; }
public virtual ICollection<CompanyFile> Files { get; set; }
}
其中一个导航属性是CompanyFile,如下所示:
public class CompanyFile
{
public int CompanyFileId { get; set; }
public Guid FileContentId { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
public string OrigFileName { get; set; }
public byte[] FileContents { get; set; }
}
在代码中,有一种加载公司及其导航属性的方法,如下所示:
public async Task<Company> GetAsync(int id)
{
return await Repo
.Include("Analyst")
.Include("Reviewer")
.Include("Files")
.Where(w => w.CompanyId == id).FirstOrDefaultAsync();
}
这一切都按预期工作,但是问题在于,如果FileContents属性很大,则加载Company对象需要很长时间。有什么方法可以加载CompanyFile导航属性,但不包括FileContents属性?
编辑:
注释中提到了一个链接,但是该链接描述的是在创建单个对象而非集合时如何忽略属性。
然后我尝试了这个:
return await Repo
.Include("Analyst")
.Include("Reviewer")
.Include("Files")
.Where(w => w.CompanyId == id).
.AsEnumerable()
.Select(p => new Company
{
p. CompanyId,
[snip]
Files = new File {
CompanyFileId = p.Files.CompanyFileId,
FileContentId = p.Files.FileContentId,
CompanyId = p.Files.FileContentId,
OrigFileName = p.Files. OrigFileName
})
编译器错误:由于不实现System.Collections.IEnumerable
,因此无法使用集合初始化器初始化类型“ Company”所以我尝试了
Company company = Repo
.Include("Group")
.Include("Analyst")
.Include("Reviewer")
.Include("Reviews")
//.Include("Files")
.Include("Priorities")
.Where(w => w.CompanyId == id)
.ToList()
.FirstOrDefault();
var FileRepo = Context.Set<CompanyFile>();
var ReviewFiles = FileRepo.Where(f => f.CompanyId == id)
.Select(t => new CompanyFile {
t.CompanyId,
{snip]
t.OrigFileName
})
.ToList();
编译时错误:无法使用集合初始化程序初始化类型CompanyFile,因为它没有实现System.Collections.IEnumerable
var ReviewFiles = FileRepo.Where(f => f.CompanyId == id)
.Select(t => new CompanyFile {
CompanyId = t.CompanyId,
[snip]
OrigFileName = t.OrigFileName });
//运行时错误:消息=“无法在LINQ to Entities查询中构造实体或复杂类型'CompanyFile'。”